Code

Introduce io_prepare as a fix to plug memory leaks related to argv formatting
[tig.git] / tig.c
diff --git a/tig.c b/tig.c
index 08ef0b4e14e5e964b46b2ece681720249eab8d4c..46f289ec1335debbcff6bc0ba014455bf7d06c77 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -687,6 +687,15 @@ argv_free(const char *argv[])
                free((void *) argv[argc]);
 }
 
+static void
+argv_copy(const char *dst[], const char *src[])
+{
+       int argc;
+
+       for (argc = 0; src[argc]; argc++)
+               dst[argc] = src[argc];
+}
+
 
 /*
  * Executing external commands.
@@ -734,6 +743,13 @@ io_init(struct io *io, const char *dir, enum io_type type)
        io->dir = dir;
 }
 
+static void
+io_prepare(struct io *io, const char *dir, enum io_type type, const char *argv[])
+{
+       io_init(io, dir, type);
+       argv_copy(io->argv, argv);
+}
+
 static bool
 io_format(struct io *io, const char *dir, enum io_type type,
          const char *argv[], enum format_flags flags)
@@ -859,9 +875,7 @@ io_start(struct io *io)
 static bool
 io_run(struct io *io, const char **argv, const char *dir, enum io_type type)
 {
-       io_init(io, dir, type);
-       if (!format_argv(io->argv, argv, FORMAT_NONE))
-               return FALSE;
+       io_prepare(io, dir, type, argv);
        return io_start(io);
 }
 
@@ -876,8 +890,7 @@ io_run_bg(const char **argv)
 {
        struct io io = {};
 
-       if (!io_format(&io, NULL, IO_BG, argv, FORMAT_NONE))
-               return FALSE;
+       io_prepare(&io, NULL, IO_BG, argv);
        return io_complete(&io);
 }
 
@@ -886,29 +899,24 @@ io_run_fg(const char **argv, const char *dir)
 {
        struct io io = {};
 
-       if (!io_format(&io, dir, IO_FG, argv, FORMAT_NONE))
-               return FALSE;
+       io_prepare(&io, dir, IO_FG, argv);
        return io_complete(&io);
 }
 
 static bool
-io_run_append(const char **argv, enum format_flags flags, int fd)
+io_run_append(const char **argv, int fd)
 {
        struct io io = {};
 
-       if (!io_format(&io, NULL, IO_AP, argv, flags)) {
-               close(fd);
-               return FALSE;
-       }
-
+       io_prepare(&io, NULL, IO_AP, argv);
        io.pipe = fd;
        return io_complete(&io);
 }
 
 static bool
-io_run_rd(struct io *io, const char **argv, const char *dir, enum format_flags flags)
+io_run_rd(struct io *io, const char **argv, const char *dir)
 {
-       return io_format(io, dir, IO_RD, argv, flags) && io_start(io);
+       return io_format(io, dir, IO_RD, argv, FORMAT_NONE) && io_start(io);
 }
 
 static bool
@@ -1045,8 +1053,8 @@ io_run_buf(const char **argv, char buf[], size_t bufsize)
 {
        struct io io = {};
 
-       return io_run_rd(&io, argv, NULL, FORMAT_NONE)
-           && io_read_buf(&io, buf, bufsize);
+       io_prepare(&io, NULL, IO_RD, argv);
+       return io_start(&io) && io_read_buf(&io, buf, bufsize);
 }
 
 static int
@@ -1093,8 +1101,8 @@ io_run_load(const char **argv, const char *separators,
 {
        struct io io = {};
 
-       return io_format(&io, NULL, IO_RD, argv, FORMAT_NONE)
-               ? io_load(&io, separators, read_property) : ERR;
+       io_prepare(&io, NULL, IO_RD, argv);
+       return io_load(&io, separators, read_property);
 }
 
 
@@ -4527,7 +4535,7 @@ tree_read_date(struct view *view, char *text, bool *read_date)
                        return TRUE;
                }
 
-               if (!io_run_rd(&io, log_file, opt_cdup, FORMAT_NONE)) {
+               if (!io_run_rd(&io, log_file, opt_cdup)) {
                        report("Failed to load tree data");
                        return TRUE;
                }
@@ -4670,7 +4678,7 @@ open_blob_editor(const char *id)
 
        if (fd == -1)
                report("Failed to create temporary file");
-       else if (!io_run_append(blob_argv, FORMAT_NONE, fd))
+       else if (!io_run_append(blob_argv, fd))
                report("Failed to save blob data to file");
        else
                open_editor(file);
@@ -4914,7 +4922,7 @@ blame_open(struct view *view)
                };
 
                if (!string_format(path, "%s:%s", opt_ref, opt_file) ||
-                   !io_run_rd(&view->io, blame_cat_file_argv, opt_cdup, FORMAT_NONE))
+                   !io_run_rd(&view->io, blame_cat_file_argv, opt_cdup))
                        return FALSE;
        }
 
@@ -5013,7 +5021,7 @@ blame_read_file(struct view *view, const char *line, bool *read_file)
                if (view->lines == 0 && !view->prev)
                        die("No blame exist for %s", view->vid);
 
-               if (view->lines == 0 || !io_run_rd(&io, blame_argv, opt_cdup, FORMAT_NONE)) {
+               if (view->lines == 0 || !io_run_rd(&io, blame_argv, opt_cdup)) {
                        report("Failed to load blame data");
                        return TRUE;
                }
@@ -5447,7 +5455,7 @@ branch_open(struct view *view)
                        "--simplify-by-decoration", "--all", NULL
        };
 
-       if (!io_run_rd(&view->io, branch_log, NULL, FORMAT_NONE)) {
+       if (!io_run_rd(&view->io, branch_log, NULL)) {
                report("Failed to load branch data");
                return TRUE;
        }