]> git.ipfire.org Git - thirdparty/git.git/commitdiff
range-diff: plug memory leak in read_patches()
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Fri, 4 Mar 2022 18:32:16 +0000 (19:32 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 4 Mar 2022 21:24:19 +0000 (13:24 -0800)
Amend code added in d9c66f0b5bf (range-diff: first rudimentary
implementation, 2018-08-13) to use a "goto cleanup" pattern. This
makes for less code, and frees memory that we'd previously leak.

The reason for changing free(util) to FREE_AND_NULL(util) is because
at the end of the function we append the contents of "util" to a
"struct string_list" if it's non-NULL.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
range-diff.c

index b2a2961f52159636c7b7b5199ce8e83e7b806d5c..b72eb9fdbee8d8d7473c973973e88eb09e15f723 100644 (file)
@@ -40,6 +40,7 @@ static int read_patches(const char *range, struct string_list *list,
        char *line, *current_filename = NULL;
        ssize_t len;
        size_t size;
+       int ret = -1;
 
        strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
                     "--reverse", "--date-order", "--decorate=no",
@@ -68,10 +69,10 @@ static int read_patches(const char *range, struct string_list *list,
        if (strbuf_read(&contents, cp.out, 0) < 0) {
                error_errno(_("could not read `log` output"));
                finish_command(&cp);
-               return -1;
+               goto cleanup;
        }
        if (finish_command(&cp))
-               return -1;
+               goto cleanup;
 
        line = contents.buf;
        size = contents.len;
@@ -95,12 +96,9 @@ static int read_patches(const char *range, struct string_list *list,
                        CALLOC_ARRAY(util, 1);
                        if (get_oid(p, &util->oid)) {
                                error(_("could not parse commit '%s'"), p);
-                               free(util);
-                               free(current_filename);
+                               FREE_AND_NULL(util);
                                string_list_clear(list, 1);
-                               strbuf_release(&buf);
-                               strbuf_release(&contents);
-                               return -1;
+                               goto cleanup;
                        }
                        util->matching = -1;
                        in_header = 1;
@@ -111,11 +109,8 @@ static int read_patches(const char *range, struct string_list *list,
                        error(_("could not parse first line of `log` output: "
                                "did not start with 'commit ': '%s'"),
                              line);
-                       free(current_filename);
                        string_list_clear(list, 1);
-                       strbuf_release(&buf);
-                       strbuf_release(&contents);
-                       return -1;
+                       goto cleanup;
                }
 
                if (starts_with(line, "diff --git")) {
@@ -136,12 +131,9 @@ static int read_patches(const char *range, struct string_list *list,
                        if (len < 0) {
                                error(_("could not parse git header '%.*s'"),
                                      orig_len, line);
-                               free(util);
-                               free(current_filename);
+                               FREE_AND_NULL(util);
                                string_list_clear(list, 1);
-                               strbuf_release(&buf);
-                               strbuf_release(&contents);
-                               return -1;
+                               goto cleanup;
                        }
                        strbuf_addstr(&buf, " ## ");
                        if (patch.is_new > 0)
@@ -219,6 +211,9 @@ static int read_patches(const char *range, struct string_list *list,
                strbuf_addch(&buf, '\n');
                util->diffsize++;
        }
+
+       ret = 0;
+cleanup:
        strbuf_release(&contents);
 
        if (util)
@@ -226,7 +221,7 @@ static int read_patches(const char *range, struct string_list *list,
        strbuf_release(&buf);
        free(current_filename);
 
-       return 0;
+       return ret;
 }
 
 static int patch_util_cmp(const void *dummy, const struct patch_util *a,