Skip to content

Commit c922b01

Browse files
torvaldsgitster
authored andcommitted
grep: fix segfault when "git grep '('" is given
Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d649048 commit c922b01

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

grep.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
5454
struct grep_expr *x;
5555

5656
p = *list;
57+
if (!p)
58+
return NULL;
5759
switch (p->token) {
5860
case GREP_PATTERN: /* atom */
5961
case GREP_PATTERN_HEAD:
@@ -66,8 +68,6 @@ static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
6668
case GREP_OPEN_PAREN:
6769
*list = p->next;
6870
x = compile_pattern_or(list);
69-
if (!x)
70-
return NULL;
7171
if (!*list || (*list)->token != GREP_CLOSE_PAREN)
7272
die("unmatched parenthesis");
7373
*list = (*list)->next;
@@ -83,6 +83,8 @@ static struct grep_expr *compile_pattern_not(struct grep_pat **list)
8383
struct grep_expr *x;
8484

8585
p = *list;
86+
if (!p)
87+
return NULL;
8688
switch (p->token) {
8789
case GREP_NOT:
8890
if (!p->next)
@@ -361,6 +363,8 @@ static int match_expr_eval(struct grep_opt *o,
361363
{
362364
int h = 0;
363365

366+
if (!x)
367+
die("Not a valid grep expression");
364368
switch (x->node) {
365369
case GREP_NODE_ATOM:
366370
h = match_one_pattern(o, x->u.atom, bol, eol, ctx);

t/t7002-grep.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ test_expect_success setup '
2626
git commit -m initial
2727
'
2828

29+
test_expect_success 'grep should not segfault with a bad input' '
30+
test_must_fail git grep "("
31+
'
32+
2933
for H in HEAD ''
3034
do
3135
case "$H" in

0 commit comments

Comments
 (0)