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

Re: git grep '(' = segfault

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Apr 27, 2009, 18:10 UTC
Message-ID
<alpine.LFD.2.00.0904271059060.22156@localhost.localdomain>
In-Reply-To
<37fcd2780904271046r7740ed42t3c9438e7aa93374@mail.gmail.com>
On Mon, 27 Apr 2009, Dmitry Potapov wrote:
Show 8 quoted lines
> 
> I have tried a few version of Git and got "Segmentation fault" when
> run:
> 
> $ git grep '('
> 
> I am not very familiar with grep code and don't have time to dig
> into it right now. So, maybe someone else can take a look at it.
Good job.
Something like this should fix it.
		Linus
---
 grep.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/grep.c b/grep.c
index f3a27d7..04c777a 100644
--- a/grep.c
+++ b/grep.c
@@ -72,6 +72,8 @@ static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
 	struct grep_expr *x;
 
 	p = *list;
+	if (!p)
+		return NULL;
 	switch (p->token) {
 	case GREP_PATTERN: /* atom */
 	case GREP_PATTERN_HEAD:
@@ -84,8 +86,6 @@ static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
 	case GREP_OPEN_PAREN:
 		*list = p->next;
 		x = compile_pattern_or(list);
-		if (!x)
-			return NULL;
 		if (!*list || (*list)->token != GREP_CLOSE_PAREN)
 			die("unmatched parenthesis");
 		*list = (*list)->next;
@@ -101,6 +101,8 @@ static struct grep_expr *compile_pattern_not(struct grep_pat **list)
 	struct grep_expr *x;
 
 	p = *list;
+	if (!p)
+		return NULL;
 	switch (p->token) {
 	case GREP_NOT:
 		if (!p->next)
@@ -372,6 +374,8 @@ static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
 	int h = 0;
 	regmatch_t match;
 
+	if (!x)
+		die("Not a valid grep expression");
 	switch (x->node) {
 	case GREP_NODE_ATOM:
 		h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
Previous: Dmitry PotapovNext: Erik Faye-Lund
Message 2 of 9 in “git grep '(' = segfault”
  1. Dmitry PotapovApr 27, 2009
  2. Linus TorvaldsApr 27, 2009
  3. Erik Faye-LundApr 27, 2009
  4. Linus TorvaldsApr 27, 2009
  5. Erik Faye-LundApr 27, 2009
  6. Matthieu MoyApr 27, 2009
  7. Erik Faye-LundApr 27, 2009
  8. Junio C HamanoApr 27, 2009
  9. Michał KiedrowiczApr 27, 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.