summaryrefslogtreecommitdiff
path: root/range-diff.c
diff options
context:
space:
mode:
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>2022-03-04 18:32:16 (GMT)
committerJunio C Hamano <gitster@pobox.com>2022-03-04 21:24:19 (GMT)
commit2d102c2bca1a0c50f17108189f134279cad941cd (patch)
tree520e1d7953744b5f931fe706117406cd3e560bff /range-diff.c
parent4998e93fa6e0da45440dd344419974e46d81a165 (diff)
downloadgit-2d102c2bca1a0c50f17108189f134279cad941cd.zip
git-2d102c2bca1a0c50f17108189f134279cad941cd.tar.gz
git-2d102c2bca1a0c50f17108189f134279cad941cd.tar.bz2
range-diff: plug memory leak in read_patches()
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>
Diffstat (limited to 'range-diff.c')
-rw-r--r--range-diff.c29
1 files changed, 12 insertions, 17 deletions
diff --git a/range-diff.c b/range-diff.c
index b2a2961..b72eb9f 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -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,