Skip to content

Commit fe7fe62

Browse files
rscharfegitster
authored andcommitted
grep: report missing left operand of --and
Git grep allows combining two patterns with --and. It checks and reports if the second pattern is missing when compiling the expression. A missing first pattern, however, is only reported later at match time. Thus no error is returned if no matching is done, e.g. because no file matches the also given pathspec. When that happens we get an expression tree with an GREP_NODE_AND node and a NULL pointer to the missing left child. free_pattern_expr() tries to dereference it during the cleanup at the end, which results in a segmentation fault. Fix this by verifying the presence of the left operand at expression compilation time. Reported-by: Matthew Hughes <[email protected]> Helped-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8b1a5f3 commit fe7fe62

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
@@ -774,6 +774,8 @@ static struct grep_expr *compile_pattern_and(struct grep_pat **list)
774774
x = compile_pattern_not(list);
775775
p = *list;
776776
if (p && p->token == GREP_AND) {
777+
if (!x)
778+
die("--and not preceded by pattern expression");
777779
if (!p->next)
778780
die("--and not followed by pattern expression");
779781
*list = p->next;

t/t7810-grep.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ test_description='git grep various.
88

99
. ./test-lib.sh
1010

11+
test_invalid_grep_expression() {
12+
params="$@" &&
13+
test_expect_success "invalid expression: grep $params" '
14+
test_must_fail git grep $params -- nonexisting
15+
'
16+
}
17+
1118
cat >hello.c <<EOF
1219
#include <assert.h>
1320
#include <stdio.h>
@@ -81,6 +88,8 @@ test_expect_success 'grep should not segfault with a bad input' '
8188
test_must_fail git grep "("
8289
'
8390

91+
test_invalid_grep_expression --and -e A
92+
8493
for H in HEAD ''
8594
do
8695
case "$H" in

0 commit comments

Comments
 (0)