Skip to content

Commit

Permalink
range-diff: plug memory leak in read_patches()
Browse files Browse the repository at this point in the history
Amend code added in d9c66f0 (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 <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
avar authored and gitster committed Mar 4, 2022
1 parent 4998e93 commit 2d102c2
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions range-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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")) {
Expand All @@ -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)
Expand Down Expand Up @@ -219,14 +211,17 @@ 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)
string_list_append(list, buf.buf)->util = util;
strbuf_release(&buf);
free(current_filename);

return 0;
return ret;
}

static int patch_util_cmp(const void *dummy, const struct patch_util *a,
Expand Down

0 comments on commit 2d102c2

Please sign in to comment.