apply: plug a leak in apply_data

We have an execution path in apply_data that leaks the local struct image. Plug it. This leak can be triggered with: $ echo foo >file $ git add file && git commit -m file $ echo bar >file $ git diff file >diff $ sed s/foo/frotz/ <diff >baddiff $ git apply --cached <baddiff Fixing this leak allows us to mark as leak-free the following tests: + t2016-checkout-patch.sh + t4103-apply-binary.sh + t4104-apply-boundary.sh + t4113-apply-ending.sh + t4117-apply-reject.sh + t4123-apply-shrink.sh + t4252-am-options.sh + t4258-am-quoted-cr.sh Mark them with "TEST_PASSES_SANITIZE_LEAK=true" to notice and fix promply any new leak that may be introduced and triggered by them in the future. Signed-off-by: Rubén Justo <rjusto@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rubén Justo committed Apr 23, 2024 at 00:54 UTC 71c791605365d1873ef631bfc478fcd75080a063
9 files changed +12 -1
apply.c
+3 -1
@@ -3712,8 +3712,10 @@ static int apply_data(struct apply_state *state, struct patch *patch,
3712 fprintf(stderr, _("Falling back to direct application...\n"));
3713
3714 /* Note: with --reject, apply_fragments() returns 0 */
3715 - if (patch->direct_to_threeway || apply_fragments(state, &image, patch) < 0)
3715 + if (patch->direct_to_threeway || apply_fragments(state, &image, patch) < 0) {
3716 + clear_image(&image);
3717 return -1;
3718 + }
3719 }
3720 patch->result = image.buf;
3721 patch->resultsize = image.len;
t/t2016-checkout-patch.sh
+1
@@ -2,6 +2,7 @@
2
3 test_description='git checkout --patch'
4
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./lib-patch-mode.sh
7
8 test_expect_success 'setup' '
t/t4103-apply-binary.sh
+1
@@ -9,6 +9,7 @@ test_description='git apply handling binary patches
9 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
10 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
11
12 +TEST_PASSES_SANITIZE_LEAK=true
13 . ./test-lib.sh
14
15 test_expect_success 'setup' '
t/t4104-apply-boundary.sh
+1
@@ -5,6 +5,7 @@
5
6 test_description='git apply boundary tests'
7
8 +TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10
11 L="c d e f g h i j k l m n o p q r s t u v w x"
t/t4113-apply-ending.sh
+1
@@ -6,6 +6,7 @@
6 test_description='git apply trying to add an ending line.
7
8 '
9 +TEST_PASSES_SANITIZE_LEAK=true
10 . ./test-lib.sh
11
12 # setup
t/t4117-apply-reject.sh
+1
@@ -7,6 +7,7 @@ test_description='git apply with rejects
7
8 '
9
10 +TEST_PASSES_SANITIZE_LEAK=true
11 . ./test-lib.sh
12
13 test_expect_success setup '
t/t4123-apply-shrink.sh
+1
@@ -2,6 +2,7 @@
2
3 test_description='apply a patch that is larger than the preimage'
4
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 cat >F <<\EOF
t/t4252-am-options.sh
+2
@@ -1,6 +1,8 @@
1 #!/bin/sh
2
3 test_description='git am with options and not losing them'
4 +
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 tm="$TEST_DIRECTORY/t4252"
t/t4258-am-quoted-cr.sh
+1
@@ -2,6 +2,7 @@
2
3 test_description='test am --quoted-cr=<action>'
4
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 DATA="$TEST_DIRECTORY/t4258"