Skip to content

Commit 61133e6

Browse files
pks-tgitster
authored andcommitted
shallow: fix leak when unregistering last shallow root
When unregistering a shallow root we shrink the array of grafts by one and move remaining grafts one to the left. This can of course only happen when there are any grafts left, because otherwise there is nothing to move. As such, this code is guarded by a condition that only performs the move in case there are grafts after the position of the graft to be unregistered. By mistake we also put the call to free the unregistered graft into that condition. But that doesn't make any sense, as we want to always free the graft when it exists. Fix the resulting memory leak by doing so. This leak is exposed by t5500, but plugging it does not make the whole test suite pass. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2ccf570 commit 61133e6

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

shallow.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ int unregister_shallow(const struct object_id *oid)
5151
int pos = commit_graft_pos(the_repository, oid);
5252
if (pos < 0)
5353
return -1;
54-
if (pos + 1 < the_repository->parsed_objects->grafts_nr) {
55-
free(the_repository->parsed_objects->grafts[pos]);
54+
free(the_repository->parsed_objects->grafts[pos]);
55+
if (pos + 1 < the_repository->parsed_objects->grafts_nr)
5656
MOVE_ARRAY(the_repository->parsed_objects->grafts + pos,
5757
the_repository->parsed_objects->grafts + pos + 1,
5858
the_repository->parsed_objects->grafts_nr - pos - 1);
59-
}
6059
the_repository->parsed_objects->grafts_nr--;
6160
return 0;
6261
}

0 commit comments

Comments
 (0)