git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] log: require --grep for --invert-grep and --all-match, fix segfault

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Oct 10, 2022, 16:57 UTC
Message-ID
<patch-1.1-f4b90799fce-20221010T165711Z-avarab@gmail.com>
In-Reply-To
<o2ijhZAUIi7nPTwQmsJfpvPIEeLbhvn1AP8rhW2hCNbC380Z2ogDHzZigqJsHI6RwphsIZR3OSJSy-wYvyWv5un632tKynHKFLFPLPEDH2g=@protonmail.com>

Neither the "--invert-grep" option added in [1] nor the earlier "--all-match" option added in [2] were intended to be used stand-alone.

But due to how the built-in and the revision API interacted those options without the corresponding --grep would be ignored.

Then in f41fb662f57 (revisions API: have release_revisions() release "grep_filter", 2022-04-13) this turned into a segfault, as we'd attempt to free() the non-existing --grep patterns.

Arguably it makes more sense to add this check to compile_grep_patterns(), since it's possible to use the C API in the same way and trigger this segfault. But in practice the revision.c API is the only user of "no_body_match", and by placing the check here we can more sensibly emit a message that assumes that the user used "--invert-grep" without "--grep".

1. 22dfa8a23de (log: teach --invert-grep option, 2015-01-12)
2. 0ab7befa31d (grep --all-match, 2006-09-27)
Reported-by: orygaw <orygaw@protonmail.com>
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 revision.c      | 6 ++++++
 t/t4202-log.sh  | 4 ++++
 t/t7810-grep.sh | 4 ++++
 3 files changed, 14 insertions(+)
diff --git a/revision.c b/revision.c
index 36e31942cee..a55ead48448 100644
--- a/revision.c
+++ b/revision.c
@@ -2986,6 +2986,12 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
 		die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
 	if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
 		die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
+	if (!revs->grep_filter.pattern_expression) {
+		if (revs->grep_filter.no_body_match)
+			die(_("the option '%s' requires '%s'"), "--invert-grep", "--grep");
+		if (revs->grep_filter.all_match)
+			die(_("the option '%s' requires '%s'"), "--all-match", "--grep");
+	}
 
 	if (revs->line_level_traverse &&
 	    (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
diff --git a/t/t4202-log.sh b/t/t4202-log.sh
index cc15cb4ff62..298678fb7c8 100755
--- a/t/t4202-log.sh
+++ b/t/t4202-log.sh
@@ -249,6 +249,10 @@ test_expect_success 'log --grep' '
 	test_cmp expect actual
 '
 
+test_expect_success 'log --invert-grep usage' '
+	test_expect_code 128 git log --invert-grep
+'
+
 cat > expect << EOF
 second
 initial
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 8eded6ab274..6dd750349e1 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -914,6 +914,10 @@ test_expect_success 'log with multiple --grep uses union' '
 	test_cmp expect actual
 '
 
+test_expect_success 'log --all-match usage' '
+	test_expect_code 128 git log --all-match
+'
+
 test_expect_success 'log --all-match with multiple --grep uses intersection' '
 	git log --all-match --grep=i --grep=r --format=%s >actual &&
 	{
-- 
2.38.0.971.ge79ff6d20e7
Previous: Junio C HamanoNext: Junio C Hamano
Message 5 of 18 in “Git BUG 2.37.3 and 2.38.0”
  1. orygawOct 10, 2022
  2. rsbecker@nexbridge.comOct 10, 2022
  3. Taylor BlauOct 10, 2022
  4. Junio C HamanoOct 10, 2022
  5. log: require --grep for --invert-grep and --all-match, fix segfaultÆvar Arnfjörð Bjarmason, Oct 10, 2022
  6. Junio C HamanoOct 10, 2022
  7. Junio C HamanoOct 10, 2022
  8. Ævar Arnfjörð BjarmasonOct 10, 2022
  9. Taylor BlauOct 10, 2022
  10. grep.c: remove "extended" in favor of "pattern_expression", fix segfaultÆvar Arnfjörð Bjarmason, Oct 11, 2022
  11. Junio C HamanoOct 11, 2022
  12. 0/2 grep: tolerate NULL argument to free_grep_expr()Taylor Blau, Oct 10, 2022
  13. 1/2 t4202: demonstrate `git log --invert-grep` segfaultTaylor Blau, Oct 10, 2022
  14. 2/2 grep.c: tolerate NULL grep_expr in free_pattern_expr()Taylor Blau, Oct 10, 2022
  15. Junio C HamanoOct 10, 2022
  16. Taylor BlauOct 10, 2022
  17. Junio C HamanoOct 10, 2022
  18. Taylor BlauOct 10, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.