Skip to content

Commit 7cb705c

Browse files
committed
transports: smart: fix memory leak when skipping symbolic refs
When we setup the revision walk for negotiating references with a remote, we iterate over all references, ignoring tags and symbolic references. While skipping over symbolic references, we forget to free the looked up reference, resulting in a memory leak when the next iteration simply overwrites the variable. Fix that issue by freeing the reference at the beginning of each iteration and collapsing return paths for error and success.
1 parent c7c5f2c commit 7cb705c

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/transports/smart_protocol.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ static int fetch_setup_walk(git_revwalk **out, git_repository *repo)
273273
git_revwalk *walk = NULL;
274274
git_strarray refs;
275275
unsigned int i;
276-
git_reference *ref;
276+
git_reference *ref = NULL;
277277
int error;
278278

279279
if ((error = git_reference_list(&refs, repo)) < 0)
@@ -285,6 +285,9 @@ static int fetch_setup_walk(git_revwalk **out, git_repository *repo)
285285
git_revwalk_sorting(walk, GIT_SORT_TIME);
286286

287287
for (i = 0; i < refs.count; ++i) {
288+
git_reference_free(ref);
289+
ref = NULL;
290+
288291
/* No tags */
289292
if (!git__prefixcmp(refs.strings[i], GIT_REFS_TAGS_DIR))
290293
continue;
@@ -297,16 +300,13 @@ static int fetch_setup_walk(git_revwalk **out, git_repository *repo)
297300

298301
if ((error = git_revwalk_push(walk, git_reference_target(ref))) < 0)
299302
goto on_error;
300-
301-
git_reference_free(ref);
302303
}
303304

304-
git_strarray_free(&refs);
305305
*out = walk;
306-
return 0;
307306

308307
on_error:
309-
git_revwalk_free(walk);
308+
if (error)
309+
git_revwalk_free(walk);
310310
git_reference_free(ref);
311311
git_strarray_free(&refs);
312312
return error;

0 commit comments

Comments
 (0)