Skip to content

Commit 2e6904a

Browse files
dschoGit for Windows Build Agent
authored andcommitted
add_reflog_for_walk: avoid memory leak
We free()d the `log` buffer when dwim_log() returned 1, but not when it returned a larger value (which meant that it still allocated the buffer but we simply ignored it). While in the vicinity, make sure that the `reflogs` structure as well as the `branch` variable are released properly, too. Identified by Coverity. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 14dd698 commit 2e6904a

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

reflog-walk.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
183183
if (!reflogs || reflogs->nr == 0) {
184184
struct object_id oid;
185185
char *b;
186-
if (dwim_log(branch, strlen(branch), oid.hash, &b) == 1) {
186+
int ret = dwim_log(branch, strlen(branch),
187+
oid.hash, &b);
188+
if (ret > 1)
189+
free(b);
190+
else if (ret == 1) {
187191
if (reflogs) {
188192
free(reflogs->ref);
189193
free(reflogs);
@@ -193,17 +197,27 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
193197
reflogs = read_complete_reflog(branch);
194198
}
195199
}
196-
if (!reflogs || reflogs->nr == 0)
200+
if (!reflogs || reflogs->nr == 0) {
201+
if (reflogs) {
202+
free(reflogs->ref);
203+
free(reflogs);
204+
}
205+
free(branch);
197206
return -1;
207+
}
198208
string_list_insert(&info->complete_reflogs, branch)->util
199209
= reflogs;
200210
}
211+
free(branch);
201212

202213
commit_reflog = xcalloc(1, sizeof(struct commit_reflog));
203214
if (recno < 0) {
204215
commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
205216
if (commit_reflog->recno < 0) {
206-
free(branch);
217+
if (reflogs) {
218+
free(reflogs->ref);
219+
free(reflogs);
220+
}
207221
free(commit_reflog);
208222
return -1;
209223
}

0 commit comments

Comments
 (0)