Fix: session name max length was not handled correctly
authorDavid Goulet <dgoulet@efficios.com>
Mon, 25 Mar 2013 18:41:22 +0000 (14:41 -0400)
committerDavid Goulet <dgoulet@efficios.com>
Mon, 25 Mar 2013 18:41:22 +0000 (14:41 -0400)
Fixes #435

Signed-off-by: David Goulet <dgoulet@efficios.com>
src/bin/lttng/commands/create.c
src/bin/lttng/commands/set_session.c
src/bin/lttng/conf.c

index 5588d4267bae2d304facb6ea73866f175f46e2e8..b7d9af52ef4e9e51a46a97c9098411a488306f06 100644 (file)
@@ -183,7 +183,7 @@ static int create_session(void)
        int ret;
        char *session_name = NULL, *traces_path = NULL, *alloc_path = NULL;
        char *alloc_url = NULL, *url = NULL, datetime[16];
-       char session_name_date[NAME_MAX], *print_str_url = NULL;
+       char session_name_date[NAME_MAX + 17], *print_str_url = NULL;
        time_t rawtime;
        struct tm *timeinfo;
 
@@ -203,6 +203,12 @@ static int create_session(void)
                session_name = session_name_date;
                DBG("Auto session name set to %s", session_name_date);
        } else {
+               if (strlen(opt_session_name) > NAME_MAX) {
+                       ERR("Session name too long. Length must be lower or equal to %d",
+                                       NAME_MAX);
+                       ret = LTTNG_ERR_SESSION_FAIL;
+                       goto error;
+               }
                if (strncmp(opt_session_name, DEFAULT_SESSION_NAME,
                                        strlen(DEFAULT_SESSION_NAME)) == 0 &&
                                strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME)) {
index a428aed16ce239257253ae2f1fd7178fc4ff69e8..1f79d6cf08e6f17c97f972a58269cb0b102362f9 100644 (file)
@@ -60,6 +60,13 @@ static int set_session(void)
 {
        int ret = CMD_SUCCESS;
 
+       if (opt_session_name && strlen(opt_session_name) > NAME_MAX) {
+               ERR("Session name too long. Length must be lower or equal to %d",
+                       NAME_MAX);
+               ret = CMD_ERROR;
+               goto error;
+       }
+
        ret = config_init(opt_session_name);
        if (ret < 0) {
                ERR("Unable to set session name");
index 7439c1cdcf4d0fac711f7a5f4acfbd018a83a773..b6632fcb144c052fa0ef4a9652843a074abd64c7 100644 (file)
@@ -40,6 +40,7 @@ char *config_get_file_path(char *path)
        ret = asprintf(&file_path, "%s/%s", path, CONFIG_FILENAME);
        if (ret < 0) {
                ERR("Fail allocating config file path");
+               file_path = NULL;
        }
 
        return file_path;
@@ -248,14 +249,16 @@ found:
 int config_add_session_name(char *path, char *name)
 {
        int ret;
-       char session_name[NAME_MAX];
+       char *attr = "session=";
+       /* Max name len accepted plus attribute's len and the NULL byte. */
+       char session_name[NAME_MAX + strlen(attr) + 1];
 
        /*
         * With GNU C <  2.1, snprintf returns -1 if the target buffer is too small;
         * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
         */
-       ret = snprintf(session_name, NAME_MAX, "session=%s\n", name);
-       if ((ret < 0) || (ret >= NAME_MAX)) {
+       ret = snprintf(session_name, sizeof(session_name), "%s%s\n", attr, name);
+       if (ret < 0) {
                ret = -1;
                goto error;
        }
This page took 0.027894 seconds and 4 git commands to generate.