grep: fix segfault when "git grep '('" is given
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Linus Torvalds committed
Apr 27, 2009 at 11:10 UTC
c922b01f54c4bebe84daeacf014cfbc8dc68479b
2 files changed
+10
-2
grep.c
+6
-2
@@ -54,6 +54,8 @@ static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
54
struct grep_expr *x;
55
56
p = *list;
57
+ if (!p)
58
+ return NULL;
59
switch (p->token) {
60
case GREP_PATTERN: /* atom */
61
case GREP_PATTERN_HEAD:
@@ -66,8 +68,6 @@ static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
68
case GREP_OPEN_PAREN:
69
*list = p->next;
70
x = compile_pattern_or(list);
69
- if (!x)
70
- return NULL;
71
if (!*list || (*list)->token != GREP_CLOSE_PAREN)
72
die("unmatched parenthesis");
73
*list = (*list)->next;
@@ -83,6 +83,8 @@ static struct grep_expr *compile_pattern_not(struct grep_pat **list)
83
struct grep_expr *x;
84
85
p = *list;
86
+ if (!p)
87
+ return NULL;
88
switch (p->token) {
89
case GREP_NOT:
90
if (!p->next)
@@ -361,6 +363,8 @@ static int match_expr_eval(struct grep_opt *o,
363
{
364
int h = 0;
365
366
+ if (!x)
367
+ die("Not a valid grep expression");
368
switch (x->node) {
369
case GREP_NODE_ATOM:
370
h = match_one_pattern(o, x->u.atom, bol, eol, ctx);
t/t7002-grep.sh
+4
@@ -26,6 +26,10 @@ test_expect_success setup '
26
git commit -m initial
27
'
28
29
+test_expect_success 'grep should not segfault with a bad input' '
30
+ test_must_fail git grep "("
31
+'
32
+
33
for H in HEAD ''
34
do
35
case "$H" in