Skip to content

Commit ea47803

Browse files
peffgitster
authored andcommitted
fetch: free "raw" string when shrinking refspec
The "--prefetch" option to git-fetch modifies the default refspec, including eliminating some entries entirely. When we drop an entry we free the strings in the refspec_item, but we forgot to free the matching string in the "raw" array of the refspec struct. There's no behavioral bug here (since we correctly shrink the raw array, too), but we're leaking the allocated string. Let's add in the leak-fix, and while we're at it drop "const" from the type of the raw string array. These strings are always allocated by refspec_append(), etc, and this makes the memory ownership more clear. This is all a bit more intimate with the refspec code than I'd like, and I suspect it would be better if each refspec_item held on to its own raw string, we had a single array, and we could use refspec_item_clear() to clean up everything. But that's a non-trivial refactoring, since refspec_item structs can be held outside of a "struct refspec", without having a matching raw string at all. So let's leave that for now and just fix the leak in the most immediate way. This lets us mark t5582 as leak-free. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e00e1cf commit ea47803

File tree

4 files changed

+4
-2
lines changed

4 files changed

+4
-2
lines changed

builtin/fetch.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ static void filter_prefetch_refspec(struct refspec *rs)
456456

457457
free(rs->items[i].src);
458458
free(rs->items[i].dst);
459+
free(rs->raw[i]);
459460

460461
for (j = i + 1; j < rs->nr; j++) {
461462
rs->items[j - 1] = rs->items[j];

refspec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ void refspec_clear(struct refspec *rs)
225225
rs->nr = 0;
226226

227227
for (i = 0; i < rs->raw_nr; i++)
228-
free((char *)rs->raw[i]);
228+
free(rs->raw[i]);
229229
FREE_AND_NULL(rs->raw);
230230
rs->raw_alloc = 0;
231231
rs->raw_nr = 0;

refspec.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct refspec {
4343
int alloc;
4444
int nr;
4545

46-
const char **raw;
46+
char **raw;
4747
int raw_alloc;
4848
int raw_nr;
4949

t/t5582-fetch-negative-refspec.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ test_description='"git fetch" with negative refspecs.
88
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
99
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
1010

11+
TEST_PASSES_SANITIZE_LEAK=true
1112
. ./test-lib.sh
1213

1314
test_expect_success setup '

0 commit comments

Comments
 (0)