Fix: Double free in utils_partial_realpath error path
[lttng-tools.git] / src / common / utils.c
index 6e772671451aec4e90e9943dd4c78eddd087c083..901ddb036771d77ff7ab1c2f723c98db7f895bbb 100644 (file)
@@ -189,6 +189,10 @@ char *utils_partial_realpath(const char *path, char *resolved_path, size_t size)
 error:
        free(resolved_path);
        free(cut_path);
+       free(try_path);
+       if (try_path_prev != try_path) {
+               free(try_path_prev);
+       }
        return NULL;
 }
 
@@ -843,7 +847,6 @@ int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size,
 {
        int ret;
 
-       assert(new_count);
        assert(stream_fd);
 
        ret = close(out_fd);
@@ -866,18 +869,22 @@ int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size,
                 * Unlinking the old file rather than overwriting it
                 * achieves this.
                 */
-               *new_count = (*new_count + 1) % count;
-               ret = utils_unlink_stream_file(path_name, file_name,
-                               size, *new_count, uid, gid, 0);
+               if (new_count) {
+                       *new_count = (*new_count + 1) % count;
+               }
+               ret = utils_unlink_stream_file(path_name, file_name, size,
+                               new_count ? *new_count : 0, uid, gid, 0);
                if (ret < 0 && errno != ENOENT) {
                        goto error;
                }
        } else {
-               (*new_count)++;
+               if (new_count) {
+                       (*new_count)++;
+               }
        }
 
-       ret = utils_create_stream_file(path_name, file_name, size, *new_count,
-                       uid, gid, 0);
+       ret = utils_create_stream_file(path_name, file_name, size,
+                       new_count ? *new_count : 0, uid, gid, 0);
        if (ret < 0) {
                goto error;
        }
@@ -1322,3 +1329,56 @@ end:
        }
        return ret;
 }
+
+LTTNG_HIDDEN
+int utils_truncate_stream_file(int fd, off_t length)
+{
+       int ret;
+
+       ret = ftruncate(fd, length);
+       if (ret < 0) {
+               PERROR("ftruncate");
+               goto end;
+       }
+       ret = lseek(fd, length, SEEK_SET);
+       if (ret < 0) {
+               PERROR("lseek");
+               goto end;
+       }
+end:
+       return ret;
+}
+
+static const char *get_man_bin_path(void)
+{
+       char *env_man_path = lttng_secure_getenv(DEFAULT_MAN_BIN_PATH_ENV);
+
+       if (env_man_path) {
+               return env_man_path;
+       }
+
+       return DEFAULT_MAN_BIN_PATH;
+}
+
+LTTNG_HIDDEN
+int utils_show_man_page(int section, const char *page_name)
+{
+       char section_string[8];
+       const char *man_bin_path = get_man_bin_path();
+       int ret;
+
+       /* Section integer -> section string */
+       ret = sprintf(section_string, "%d", section);
+       assert(ret > 0 && ret < 8);
+
+       /*
+        * Execute man pager.
+        *
+        * We provide --manpath to man here because LTTng-tools can
+        * be installed outside /usr, in which case its man pages are
+        * not located in the default /usr/share/man directory.
+        */
+       ret = execlp(man_bin_path, "man", "--manpath", MANPATH,
+               section_string, page_name, NULL);
+       return ret;
+}
This page took 0.023987 seconds and 4 git commands to generate.