]> git.ipfire.org Git - thirdparty/git.git/commitdiff
diff: fix leak when parsing invalid ignore regex option
authorPatrick Steinhardt <ps@pks.im>
Wed, 14 Aug 2024 06:52:42 +0000 (08:52 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 14 Aug 2024 17:08:00 +0000 (10:08 -0700)
When parsing invalid ignore regexes passed via the `-I` option we don't
free already-allocated memory, leading to a memory leak. Fix this.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff.c
t/t4013-diff-various.sh

diff --git a/diff.c b/diff.c
index ebb7538e040a716225b9cfb4852378bec07bb66a..9251c47b728799899adb4be9b462179b1807d198 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -5464,9 +5464,13 @@ static int diff_opt_ignore_regex(const struct option *opt,
        regex_t *regex;
 
        BUG_ON_OPT_NEG(unset);
+
        regex = xmalloc(sizeof(*regex));
-       if (regcomp(regex, arg, REG_EXTENDED | REG_NEWLINE))
+       if (regcomp(regex, arg, REG_EXTENDED | REG_NEWLINE)) {
+               free(regex);
                return error(_("invalid regex given to -I: '%s'"), arg);
+       }
+
        ALLOC_GROW(options->ignore_regex, options->ignore_regex_nr + 1,
                   options->ignore_regex_alloc);
        options->ignore_regex[options->ignore_regex_nr++] = regex;
index 3855d68dbc0a64797ddbd665e675aa3d1549defc..87d248d0347ae0b8e37bac315a9bb84aa4e225ec 100755 (executable)
@@ -8,6 +8,7 @@ test_description='Various diff formatting options'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-diff.sh