summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--grep.c2
-rwxr-xr-xt/t7810-grep.sh9
2 files changed, 11 insertions, 0 deletions
diff --git a/grep.c b/grep.c
index 4db1510d16..71e44402dc 100644
--- a/grep.c
+++ b/grep.c
@@ -774,6 +774,8 @@ static struct grep_expr *compile_pattern_and(struct grep_pat **list)
x = compile_pattern_not(list);
p = *list;
if (p && p->token == GREP_AND) {
+ if (!x)
+ die("--and not preceded by pattern expression");
if (!p->next)
die("--and not followed by pattern expression");
*list = p->next;
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 43aa4161cf..47434e6687 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -8,6 +8,13 @@ test_description='git grep various.
. ./test-lib.sh
+test_invalid_grep_expression() {
+ params="$@" &&
+ test_expect_success "invalid expression: grep $params" '
+ test_must_fail git grep $params -- nonexisting
+ '
+}
+
cat >hello.c <<EOF
#include <assert.h>
#include <stdio.h>
@@ -81,6 +88,8 @@ test_expect_success 'grep should not segfault with a bad input' '
test_must_fail git grep "("
'
+test_invalid_grep_expression --and -e A
+
for H in HEAD ''
do
case "$H" in