diff: fix leak when parsing invalid ignore regex option

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>

Patrick Steinhardt committed Aug 14, 2024 at 08:52 UTC 6b15d9ca7f555b7366ed810e2334d614b96361ad
2 files changed +6 -1
diff.c
+5 -1
@@ -5464,9 +5464,13 @@ static int diff_opt_ignore_regex(const struct option *opt,
5464 regex_t *regex;
5465
5466 BUG_ON_OPT_NEG(unset);
5467 +
5468 regex = xmalloc(sizeof(*regex));
5468 - if (regcomp(regex, arg, REG_EXTENDED | REG_NEWLINE))
5469 + if (regcomp(regex, arg, REG_EXTENDED | REG_NEWLINE)) {
5470 + free(regex);
5471 return error(_("invalid regex given to -I: '%s'"), arg);
5472 + }
5473 +
5474 ALLOC_GROW(options->ignore_regex, options->ignore_regex_nr + 1,
5475 options->ignore_regex_alloc);
5476 options->ignore_regex[options->ignore_regex_nr++] = regex;
t/t4013-diff-various.sh
+1
@@ -8,6 +8,7 @@ test_description='Various diff formatting options'
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
11 +TEST_PASSES_SANITIZE_LEAK=true
12 . ./test-lib.sh
13 . "$TEST_DIRECTORY"/lib-diff.sh
14