Fix: save session usage of string size
[lttng-tools.git] / src / bin / lttng-sessiond / save.c
index 8599d2d18a6796bc3c6ba90017f4ea55420c0d6d..0c91239931ad6125049687ea411fad8c52bddea7 100644 (file)
@@ -1419,12 +1419,13 @@ int save_session(struct ltt_session *session,
        provided_path = lttng_save_session_attr_get_output_url(attr);
        if (provided_path) {
                len = strlen(provided_path);
-               if (len > PATH_MAX) {
+               if (len >= sizeof(config_file_path)) {
                        ret = LTTNG_ERR_SET_URL;
                        goto end;
                }
                strncpy(config_file_path, provided_path, len);
        } else {
+               ssize_t ret_len;
                char *home_dir = utils_get_user_home_dir(
                        LTTNG_SOCK_GET_UID_CRED(creds));
                if (!home_dir) {
@@ -1432,22 +1433,24 @@ int save_session(struct ltt_session *session,
                        goto end;
                }
 
-               len = snprintf(config_file_path, PATH_MAX,
+               ret_len = snprintf(config_file_path, sizeof(config_file_path),
                                DEFAULT_SESSION_HOME_CONFIGPATH, home_dir);
                free(home_dir);
-               if (len < 0) {
+               if (ret_len < 0) {
                        PERROR("snprintf save session");
                        ret = LTTNG_ERR_SET_URL;
                        goto end;
                }
+               len = ret_len;
        }
 
        /*
-        * Check the path fits in PATH_MAX, including the / followed by trailing
-        * .lttng extension and the NULL terminated string.
+        * Check the path fits in the config file path dst including the '/'
+        * followed by trailing .lttng extension and the NULL terminated string.
         */
-       if (len + session_name_len + 2 +
-                       sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION) > PATH_MAX) {
+       if ((len + session_name_len + 2 +
+                       sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION))
+                       > sizeof(config_file_path)) {
                ret = LTTNG_ERR_SET_URL;
                goto end;
        }
@@ -1459,6 +1462,10 @@ int save_session(struct ltt_session *session,
                goto end;
        }
 
+       /*
+        * At this point, we know that everything fits in the buffer. Validation
+        * was done just above.
+        */
        config_file_path[len++] = '/';
        strncpy(config_file_path + len, session->name, session_name_len);
        len += session_name_len;
This page took 0.023949 seconds and 4 git commands to generate.