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

[PATCH v2] grep: provide sane default to grep_source struct

From
Emily Shaffer <emilyshaffer@google.com>
Date
May 16, 2019, 21:44 UTC
Message-ID
<20190516214444.191743-1-emilyshaffer@google.com>
In-Reply-To
<20190516020023.61161-1-emilyshaffer@google.com>

grep_buffer creates a struct grep_source gs and calls grep_source() with it. However, gs.name is null, which eventually produces a segmentation fault in grep_source()->grep_source_1()->show_line() when grep_opt.status_only is not set.

This seems to be unreachable from existing commands but is reachable in the event that someone rolls their own revision walk and neglects to set rev_info->grep_filter->status_only. Conceivably, someone might want to print all changed lines of all commits which match some regex.

To futureproof, give developers a heads-up if grep_source() is called and a valid name field is expected but not given. This path should be unreachable now, but in the future may become reachable if developers want to add the ability to use grep_buffer() in prepared in-core data and provide hints to the user about where the data came from.

Signed-off-by: Emily Shaffer <emilyshaffer@google.com>
---
I've added the BUG() line to grep_source(). I considered adding it to
grep_source_1() but didn't see much difference. Looks like
grep_source_1() exists purely as a helper to grep_source() and is never
called by anybody else... but it's the function where the crash would
happen. So I'm not sure.

I also modified the wording for the BUG("") statement a little from jrn's suggestion to hopefully make it more explicit, but I welcome wordsmithing.

 grep.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/grep.c b/grep.c
index 0d50598acd..6ceb880a8c 100644
--- a/grep.c
+++ b/grep.c
@@ -2021,6 +2021,9 @@ static int chk_hit_marker(struct grep_expr *x)
 
 int grep_source(struct grep_opt *opt, struct grep_source *gs)
 {
+	if (!opt->status_only && gs->name == NULL)
+		BUG("grep call which could print a name requires "
+		    "grep_source.name be non-NULL");
 	/*
 	 * we do not have to do the two-pass grep when we do not check
 	 * buffer-wide "all-match".
@@ -2045,7 +2048,11 @@ int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
 	struct grep_source gs;
 	int r;
 
-	grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL, NULL);
+	/* TODO: In the future it may become desirable to pass in the name as
+	 * an argument to grep_buffer(). At that time, "(in core)" should be
+	 * replaced.
+	 */
+	grep_source_init(&gs, GREP_SOURCE_BUF, _("(in core)"), NULL, NULL);
 	gs.buf = buf;
 	gs.size = size;
 
-- 
2.21.0.1020.gf2820cf01a-goog
Previous: Emily ShafferNext: Jeff King
Message 6 of 15 in “grep: provide sane default to grep_source struct”
  1. grep: provide sane default to grep_source structEmily Shaffer, May 16, 2019
  2. Junio C HamanoMay 16, 2019
  3. Jonathan NiederMay 16, 2019
  4. Jonathan NiederMay 16, 2019
  5. Emily ShafferMay 16, 2019
  6. grep: provide sane default to grep_source structEmily Shaffer, May 16, 2019
  7. Jeff KingMay 16, 2019
  8. Emily ShafferMay 21, 2019
  9. Jonathan NiederMay 22, 2019
  10. grep: provide sane default to grep_source structEmily Shaffer, May 22, 2019
  11. Jonathan NiederMay 22, 2019
  12. Jeff KingMay 22, 2019
  13. grep: fail if call could output and name is nullEmily Shaffer, May 23, 2019
  14. Jonathan NiederMay 23, 2019
  15. Junio C HamanoMay 28, 2019

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.