mirror of
https://github.com/git/git.git
synced 2024-11-23 09:56:28 +08:00
combine-diff: fix leaking lost lines
The `cnt` variable tracks the number of lines in a patch diff. It can happen though that there are no newlines, in which case we'd still end up allocating our array of `sline`s. In fact, we always allocate it with `cnt + 2` entries: one extra entry for the deletion hunk at the end, and another entry that we don't seem to ever populate at all but acts as a kind of sentinel value. When we loop through the array to clear it at the end of this function we only loop until `lno < cnt`, and thus we may not end up releasing whatever the two extra `sline`s contain. While that shouldn't matter for the sentinel value, it does matter for the extra deletion hunk sline. Regardless of that, plug this memory leak by releasing both extra entries, which makes the logic a bit easier to reason about. While at it, fix the formatting of a local comment, which incidentally also provides the necessary context for why we overallocate the `sline` array. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
d06e3ec858
commit
1981d1eb3e
@ -1185,7 +1185,8 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
|
|||||||
result_file.ptr = result;
|
result_file.ptr = result;
|
||||||
result_file.size = result_size;
|
result_file.size = result_size;
|
||||||
|
|
||||||
/* Even p_lno[cnt+1] is valid -- that is for the end line number
|
/*
|
||||||
|
* Even p_lno[cnt+1] is valid -- that is for the end line number
|
||||||
* for deletion hunk at the end.
|
* for deletion hunk at the end.
|
||||||
*/
|
*/
|
||||||
CALLOC_ARRAY(sline[0].p_lno, st_mult(st_add(cnt, 2), num_parent));
|
CALLOC_ARRAY(sline[0].p_lno, st_mult(st_add(cnt, 2), num_parent));
|
||||||
@ -1220,7 +1221,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
|
|||||||
}
|
}
|
||||||
free(result);
|
free(result);
|
||||||
|
|
||||||
for (lno = 0; lno < cnt; lno++) {
|
for (lno = 0; lno < cnt + 2; lno++) {
|
||||||
if (sline[lno].lost) {
|
if (sline[lno].lost) {
|
||||||
struct lline *ll = sline[lno].lost;
|
struct lline *ll = sline[lno].lost;
|
||||||
while (ll) {
|
while (ll) {
|
||||||
|
@ -5,6 +5,7 @@ test_description='combined diff'
|
|||||||
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
|
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
|
||||||
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
|
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
|
||||||
|
|
||||||
|
TEST_PASSES_SANITIZE_LEAK=true
|
||||||
. ./test-lib.sh
|
. ./test-lib.sh
|
||||||
. "$TEST_DIRECTORY"/lib-diff.sh
|
. "$TEST_DIRECTORY"/lib-diff.sh
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user