Skip to content

Commit 2d102c2

Browse files
avargitster
authored andcommitted
range-diff: plug memory leak in read_patches()
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]>
1 parent 4998e93 commit 2d102c2

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

range-diff.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static int read_patches(const char *range, struct string_list *list,
4040
char *line, *current_filename = NULL;
4141
ssize_t len;
4242
size_t size;
43+
int ret = -1;
4344

4445
strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
4546
"--reverse", "--date-order", "--decorate=no",
@@ -68,10 +69,10 @@ static int read_patches(const char *range, struct string_list *list,
6869
if (strbuf_read(&contents, cp.out, 0) < 0) {
6970
error_errno(_("could not read `log` output"));
7071
finish_command(&cp);
71-
return -1;
72+
goto cleanup;
7273
}
7374
if (finish_command(&cp))
74-
return -1;
75+
goto cleanup;
7576

7677
line = contents.buf;
7778
size = contents.len;
@@ -95,12 +96,9 @@ static int read_patches(const char *range, struct string_list *list,
9596
CALLOC_ARRAY(util, 1);
9697
if (get_oid(p, &util->oid)) {
9798
error(_("could not parse commit '%s'"), p);
98-
free(util);
99-
free(current_filename);
99+
FREE_AND_NULL(util);
100100
string_list_clear(list, 1);
101-
strbuf_release(&buf);
102-
strbuf_release(&contents);
103-
return -1;
101+
goto cleanup;
104102
}
105103
util->matching = -1;
106104
in_header = 1;
@@ -111,11 +109,8 @@ static int read_patches(const char *range, struct string_list *list,
111109
error(_("could not parse first line of `log` output: "
112110
"did not start with 'commit ': '%s'"),
113111
line);
114-
free(current_filename);
115112
string_list_clear(list, 1);
116-
strbuf_release(&buf);
117-
strbuf_release(&contents);
118-
return -1;
113+
goto cleanup;
119114
}
120115

121116
if (starts_with(line, "diff --git")) {
@@ -136,12 +131,9 @@ static int read_patches(const char *range, struct string_list *list,
136131
if (len < 0) {
137132
error(_("could not parse git header '%.*s'"),
138133
orig_len, line);
139-
free(util);
140-
free(current_filename);
134+
FREE_AND_NULL(util);
141135
string_list_clear(list, 1);
142-
strbuf_release(&buf);
143-
strbuf_release(&contents);
144-
return -1;
136+
goto cleanup;
145137
}
146138
strbuf_addstr(&buf, " ## ");
147139
if (patch.is_new > 0)
@@ -219,14 +211,17 @@ static int read_patches(const char *range, struct string_list *list,
219211
strbuf_addch(&buf, '\n');
220212
util->diffsize++;
221213
}
214+
215+
ret = 0;
216+
cleanup:
222217
strbuf_release(&contents);
223218

224219
if (util)
225220
string_list_append(list, buf.buf)->util = util;
226221
strbuf_release(&buf);
227222
free(current_filename);
228223

229-
return 0;
224+
return ret;
230225
}
231226

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

0 commit comments

Comments
 (0)