summaryrefslogtreecommitdiff
path: root/grep.c
diff options
context:
space:
mode:
Diffstat (limited to 'grep.c')
-rw-r--r--grep.c57
1 files changed, 35 insertions, 22 deletions
diff --git a/grep.c b/grep.c
index 0904d55b24..e95cded414 100644
--- a/grep.c
+++ b/grep.c
@@ -9,7 +9,6 @@
#include "xdiff-interface.h"
#include "diff.h"
#include "diffcore.h"
-#include "commit.h"
#include "quote.h"
#include "help.h"
@@ -17,7 +16,7 @@ static int grep_source_load(struct grep_source *gs);
static int grep_source_is_binary(struct grep_source *gs,
struct index_state *istate);
-static void std_output(struct grep_opt *opt, const void *buf, size_t size)
+static void std_output(struct grep_opt *opt UNUSED, const void *buf, size_t size)
{
fwrite(buf, size, 1, stdout);
}
@@ -246,7 +245,7 @@ static int is_fixed(const char *s, size_t len)
#ifdef USE_LIBPCRE2
#define GREP_PCRE2_DEBUG_MALLOC 0
-static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data)
+static void *pcre2_malloc(PCRE2_SIZE size, void *memory_data UNUSED)
{
void *pointer = malloc(size);
#if GREP_PCRE2_DEBUG_MALLOC
@@ -256,7 +255,7 @@ static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data)
return pointer;
}
-static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data)
+static void pcre2_free(void *pointer, void *memory_data UNUSED)
{
#if GREP_PCRE2_DEBUG_MALLOC
static int count = 1;
@@ -452,18 +451,20 @@ static void free_pcre2_pattern(struct grep_pat *p)
pcre2_general_context_free(p->pcre2_general_context);
}
#else /* !USE_LIBPCRE2 */
-static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
+static void compile_pcre2_pattern(struct grep_pat *p UNUSED,
+ const struct grep_opt *opt UNUSED)
{
die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
}
-static int pcre2match(struct grep_pat *p, const char *line, const char *eol,
- regmatch_t *match, int eflags)
+static int pcre2match(struct grep_pat *p UNUSED, const char *line UNUSED,
+ const char *eol UNUSED, regmatch_t *match UNUSED,
+ int eflags UNUSED)
{
return 1;
}
-static void free_pcre2_pattern(struct grep_pat *p)
+static void free_pcre2_pattern(struct grep_pat *p UNUSED)
{
}
@@ -620,7 +621,7 @@ static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
*list = p->next;
x = compile_pattern_or(list);
if (!*list || (*list)->token != GREP_CLOSE_PAREN)
- die("unmatched parenthesis");
+ die("unmatched ( for expression group");
*list = (*list)->next;
return x;
default:
@@ -755,6 +756,7 @@ static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y
assert(x->node == GREP_NODE_OR);
if (x->u.binary.right &&
x->u.binary.right->node == GREP_NODE_TRUE) {
+ free(x->u.binary.right);
x->u.binary.right = y;
break;
}
@@ -791,7 +793,7 @@ void compile_grep_patterns(struct grep_opt *opt)
if (p)
opt->pattern_expression = compile_pattern_expr(&p);
if (p)
- die("incomplete pattern expression: %s", p->pattern);
+ die("incomplete pattern expression group: %s", p->pattern);
if (opt->no_body_match && opt->pattern_expression)
opt->pattern_expression = grep_not_expr(opt->pattern_expression);
@@ -842,11 +844,11 @@ static void free_grep_pat(struct grep_pat *pattern)
free_pcre2_pattern(p);
else
regfree(&p->regexp);
- free(p->pattern);
break;
default:
break;
}
+ free(p->pattern);
free(p);
}
}
@@ -905,15 +907,17 @@ static int patmatch(struct grep_pat *p,
const char *line, const char *eol,
regmatch_t *match, int eflags)
{
- int hit;
-
if (p->pcre2_pattern)
- hit = !pcre2match(p, line, eol, match, eflags);
- else
- hit = !regexec_buf(&p->regexp, line, eol - line, 1, match,
- eflags);
+ return !pcre2match(p, line, eol, match, eflags);
- return hit;
+ switch (regexec_buf(&p->regexp, line, eol - line, 1, match, eflags)) {
+ case 0:
+ return 1;
+ case REG_NOMATCH:
+ return 0;
+ default:
+ return -1;
+ }
}
static void strip_timestamp(const char *bol, const char **eol_p)
@@ -951,6 +955,8 @@ static int headerless_match_one_pattern(struct grep_pat *p,
again:
hit = patmatch(p, bol, eol, pmatch, eflags);
+ if (hit < 0)
+ hit = 0;
if (hit && p->word_regexp) {
if ((pmatch[0].rm_so < 0) ||
@@ -1460,6 +1466,8 @@ static int look_ahead(struct grep_opt *opt,
regmatch_t m;
hit = patmatch(p, bol, bol + *left_p, &m, 0);
+ if (hit < 0)
+ return -1;
if (!hit || m.rm_so < 0 || m.rm_eo < 0)
continue;
if (earliest < 0 || m.rm_so < earliest)
@@ -1654,9 +1662,13 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
if (try_lookahead
&& !(last_hit
&& (show_function ||
- lno <= last_hit + opt->post_context))
- && look_ahead(opt, &left, &lno, &bol))
- break;
+ lno <= last_hit + opt->post_context))) {
+ hit = look_ahead(opt, &left, &lno, &bol);
+ if (hit < 0)
+ try_lookahead = 0;
+ else if (hit)
+ break;
+ }
eol = end_of_line(bol, &left);
if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
@@ -1734,7 +1746,8 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
peek_eol = end_of_line(peek_bol, &peek_left);
}
- if (match_funcname(opt, gs, peek_bol, peek_eol))
+ if (peek_bol >= gs->buf + gs->size ||
+ match_funcname(opt, gs, peek_bol, peek_eol))
show_function = 0;
}
if (show_function ||