]> git.ipfire.org Git - thirdparty/git.git/commitdiff
apply: plug a leak in apply_data
authorRubén Justo <rjusto@gmail.com>
Mon, 22 Apr 2024 22:54:05 +0000 (00:54 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 22 Apr 2024 23:27:42 +0000 (16:27 -0700)
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>
apply.c
t/t2016-checkout-patch.sh
t/t4103-apply-binary.sh
t/t4104-apply-boundary.sh
t/t4113-apply-ending.sh
t/t4117-apply-reject.sh
t/t4123-apply-shrink.sh
t/t4252-am-options.sh
t/t4258-am-quoted-cr.sh

diff --git a/apply.c b/apply.c
index 34f20326a7f0ad2328b83af969d2c5419c2349eb..2f752d71a890248caaf1d5145cfd079bb2b790d2 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -3712,8 +3712,10 @@ static int apply_data(struct apply_state *state, struct patch *patch,
                        fprintf(stderr, _("Falling back to direct application...\n"));
 
                /* Note: with --reject, apply_fragments() returns 0 */
-               if (patch->direct_to_threeway || apply_fragments(state, &image, patch) < 0)
+               if (patch->direct_to_threeway || apply_fragments(state, &image, patch) < 0) {
+                       clear_image(&image);
                        return -1;
+               }
        }
        patch->result = image.buf;
        patch->resultsize = image.len;
index c4f9bf09aa4fd8d541a858d55cfbff34e78de1c0..c40b661ac12d57209199ea397748409860345d8f 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='git checkout --patch'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./lib-patch-mode.sh
 
 test_expect_success 'setup' '
index d370ecfe0d9eea752d81d02f50f5739bfc923b04..144619ab873f5d55ff52e5324d15ab9921941856 100755 (executable)
@@ -9,6 +9,7 @@ test_description='git apply handling binary patches
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '
index 71ef4132d153b7be4b4f3d4ebfb1a85ee4bfa9ab..dc501aac387b26aa344564dfb38f179f4601546c 100755 (executable)
@@ -5,6 +5,7 @@
 
 test_description='git apply boundary tests'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 L="c d e f g h i j k l m n o p q r s t u v w x"
index 66fa51591eb7ee8f102fd86e30e54af2da3ea310..2c65c6a16960a0429764ac628aab001fd14ed2f3 100755 (executable)
@@ -6,6 +6,7 @@
 test_description='git apply trying to add an ending line.
 
 '
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 # setup
index c86d05a96fe8d5c7a561028c8dad452f89eb6876..4d15ccd28ecb27b47041cbaab412f1f1b93a91bf 100755 (executable)
@@ -7,6 +7,7 @@ test_description='git apply with rejects
 
 '
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success setup '
index 3ef84619f53e27ddc164d74c07c0a38721dedfdf..3601c0c5dca5aa4ac364d19ce1a96f50748cc723 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='apply a patch that is larger than the preimage'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 cat >F  <<\EOF
index e758e634a34762c6a2e085e0f6ea8fc5cc00af5e..5b680dc755916ea0c165fb5de8a613679426378f 100755 (executable)
@@ -1,6 +1,8 @@
 #!/bin/sh
 
 test_description='git am with options and not losing them'
+
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 tm="$TEST_DIRECTORY/t4252"
index 201915b45a8bd67c2fc744c259b5a2ee24138796..3573c9147f17c58d822cd52904349f548d68cd63 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='test am --quoted-cr=<action>'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 DATA="$TEST_DIRECTORY/t4258"