Skip to content

Commit 1157618

Browse files
committed
Merge branch 'rs/grep-parser-fix'
"git grep --and -e foo" ought to have been diagnosed as an error but instead segfaulted, which has been corrected. * rs/grep-parser-fix: grep: report missing left operand of --and
2 parents 21ef7ee + fe7fe62 commit 1157618

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

grep.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,8 @@ static struct grep_expr *compile_pattern_and(struct grep_pat **list)
657657
x = compile_pattern_not(list);
658658
p = *list;
659659
if (p && p->token == GREP_AND) {
660+
if (!x)
661+
die("--and not preceded by pattern expression");
660662
if (!p->next)
661663
die("--and not followed by pattern expression");
662664
*list = p->next;

t/t7810-grep.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
1111

1212
. ./test-lib.sh
1313

14+
test_invalid_grep_expression() {
15+
params="$@" &&
16+
test_expect_success "invalid expression: grep $params" '
17+
test_must_fail git grep $params -- nonexisting
18+
'
19+
}
20+
1421
cat >hello.c <<EOF
1522
#include <assert.h>
1623
#include <stdio.h>
@@ -89,6 +96,8 @@ test_expect_success 'grep should not segfault with a bad input' '
8996
test_must_fail git grep "("
9097
'
9198

99+
test_invalid_grep_expression --and -e A
100+
92101
for H in HEAD ''
93102
do
94103
case "$H" in

0 commit comments

Comments
 (0)