Merge branch 'ab/grep-simplify-extended-expression'

Giving "--invert-grep" and "--all-match" without "--grep" to the
"git log" command resulted in an attempt to access grep pattern
expression structure that has not been allocated, which has been
corrected.

* ab/grep-simplify-extended-expression:
  grep.c: remove "extended" in favor of "pattern_expression", fix segfault
This commit is contained in:
Junio C Hamano 2022-10-21 11:37:28 -07:00
commit 91d3d7e6e2
3 changed files with 16 additions and 9 deletions

15
grep.c
View File

@ -708,6 +708,7 @@ void compile_grep_patterns(struct grep_opt *opt)
{ {
struct grep_pat *p; struct grep_pat *p;
struct grep_expr *header_expr = prep_header_patterns(opt); struct grep_expr *header_expr = prep_header_patterns(opt);
int extended = 0;
for (p = opt->pattern_list; p; p = p->next) { for (p = opt->pattern_list; p; p = p->next) {
switch (p->token) { switch (p->token) {
@ -717,14 +718,14 @@ void compile_grep_patterns(struct grep_opt *opt)
compile_regexp(p, opt); compile_regexp(p, opt);
break; break;
default: default:
opt->extended = 1; extended = 1;
break; break;
} }
} }
if (opt->all_match || opt->no_body_match || header_expr) if (opt->all_match || opt->no_body_match || header_expr)
opt->extended = 1; extended = 1;
else if (!opt->extended) else if (!extended)
return; return;
p = opt->pattern_list; p = opt->pattern_list;
@ -790,7 +791,7 @@ void free_grep_patterns(struct grep_opt *opt)
free(p); free(p);
} }
if (!opt->extended) if (!opt->pattern_expression)
return; return;
free_pattern_expr(opt->pattern_expression); free_pattern_expr(opt->pattern_expression);
} }
@ -971,8 +972,6 @@ static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x,
{ {
int h = 0; int h = 0;
if (!x)
die("Not a valid grep expression");
switch (x->node) { switch (x->node) {
case GREP_NODE_TRUE: case GREP_NODE_TRUE:
h = 1; h = 1;
@ -1052,7 +1051,7 @@ static int match_line(struct grep_opt *opt,
struct grep_pat *p; struct grep_pat *p;
int hit = 0; int hit = 0;
if (opt->extended) if (opt->pattern_expression)
return match_expr(opt, bol, eol, ctx, col, icol, return match_expr(opt, bol, eol, ctx, col, icol,
collect_hits); collect_hits);
@ -1370,7 +1369,7 @@ static int should_lookahead(struct grep_opt *opt)
{ {
struct grep_pat *p; struct grep_pat *p;
if (opt->extended) if (opt->pattern_expression)
return 0; /* punt for too complex stuff */ return 0; /* punt for too complex stuff */
if (opt->invert) if (opt->invert)
return 0; return 0;

1
grep.h
View File

@ -151,7 +151,6 @@ struct grep_opt {
#define GREP_BINARY_TEXT 2 #define GREP_BINARY_TEXT 2
int binary; int binary;
int allow_textconv; int allow_textconv;
int extended;
int use_reflog_filter; int use_reflog_filter;
int relative; int relative;
int pathname; int pathname;

View File

@ -249,6 +249,15 @@ test_expect_success 'log --grep' '
test_cmp expect actual test_cmp expect actual
' '
for noop_opt in --invert-grep --all-match
do
test_expect_success "log $noop_opt without --grep is a NOOP" '
git log >expect &&
git log $noop_opt >actual &&
test_cmp expect actual
'
done
cat > expect << EOF cat > expect << EOF
second second
initial initial