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

[PATCH] grep: do not segfault when -f is used

From
MKMatt Kraai <kraai@ftbfs.org>
Date
Oct 16, 2009, 08:53 UTC
Message-ID
<1255683204-28988-1-git-send-email-kraai@ftbfs.org>

"git grep" would segfault if its -f option was used because it would try to use an uninitialized strbuf, so initialize the strbuf.

Signed-off-by: Matt Kraai <kraai@ftbfs.org>
---
 builtin-grep.c  |    2 +-
 t/t7002-grep.sh |    4 ++++
 2 files changed, 5 insertions(+), 1 deletions(-)
diff --git a/builtin-grep.c b/builtin-grep.c
index 761799d..1df25b0 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -631,7 +631,7 @@ static int file_callback(const struct option *opt, const char *arg, int unset)
 	struct grep_opt *grep_opt = opt->value;
 	FILE *patterns;
 	int lno = 0;
-	struct strbuf sb;
+	struct strbuf sb = STRBUF_INIT;
 
 	patterns = fopen(arg, "r");
 	if (!patterns)
diff --git a/t/t7002-grep.sh b/t/t7002-grep.sh
index ae56a36..762f815 100755
--- a/t/t7002-grep.sh
+++ b/t/t7002-grep.sh
@@ -44,6 +44,10 @@ test_expect_success 'grep should not segfault with a bad input' '
 	test_must_fail git grep "("
 '
 
+test_expect_success 'grep should not segfault with -f' '
+        test_must_fail git grep -f /dev/null
+'
+
 for H in HEAD ''
 do
 	case "$H" in
-- 
1.6.5
Next: Johannes Sixt
Message 1 of 6 in “grep: do not segfault when -f is used”
  1. grep: do not segfault when -f is usedMatt Kraai, Oct 16, 2009
  2. Johannes SixtOct 16, 2009
  3. Matt KraaiOct 16, 2009
  4. Johannes SixtOct 16, 2009
  5. grep: do not segfault when -f is usedMatt Kraai, Oct 16, 2009
  6. Junio C HamanoOct 17, 2009

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.