summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: f96e567)
raw | patch | inline | side by side (parent: f96e567)
author | René Scharfe <rene.scharfe@lsrfire.ath.cx> | |
Sat, 22 May 2010 21:43:43 +0000 (23:43 +0200) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Mon, 24 May 2010 18:22:07 +0000 (11:22 -0700) |
Search patterns in a file specified with -f can contain NUL characters.
The current code ignores all characters on a line after a NUL.
Pass the actual length of the line all the way from the pattern file to
fixmatch() and use it for case-sensitive fixed string matching.
Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
The current code ignores all characters on a line after a NUL.
Pass the actual length of the line all the way from the pattern file to
fixmatch() and use it for case-sensitive fixed string matching.
Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/grep.c | patch | blob | history | |
grep.c | patch | blob | history | |
grep.h | patch | blob | history | |
t/t7008-grep-binary.sh | patch | blob | history |
diff --git a/builtin/grep.c b/builtin/grep.c
index 8e928e217041a159f4a962f0883d740aa84536d7..7653d8492a9f26b47cfaeec64f717107ffa204bc 100644 (file)
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -724,11 +724,15 @@ static int file_callback(const struct option *opt, const char *arg, int unset)
if (!patterns)
die_errno("cannot open '%s'", arg);
while (strbuf_getline(&sb, patterns, '\n') == 0) {
+ char *s;
+ size_t len;
+
/* ignore empty line like grep does */
if (sb.len == 0)
continue;
- append_grep_pattern(grep_opt, strbuf_detach(&sb, NULL), arg,
- ++lno, GREP_PATTERN);
+
+ s = strbuf_detach(&sb, &len);
+ append_grep_pat(grep_opt, s, len, arg, ++lno, GREP_PATTERN);
}
fclose(patterns);
strbuf_release(&sb);
index 70a776f6fece05687bd684aeb7047615aa5a0064..82fb349be6f6ef8211d4828101cd8e88bcbb304f 100644 (file)
--- a/grep.c
+++ b/grep.c
{
struct grep_pat *p = xcalloc(1, sizeof(*p));
p->pattern = pat;
+ p->patternlen = strlen(pat);
p->origin = "header";
p->no = 0;
p->token = GREP_PATTERN_HEAD;
void append_grep_pattern(struct grep_opt *opt, const char *pat,
const char *origin, int no, enum grep_pat_token t)
+{
+ append_grep_pat(opt, pat, strlen(pat), origin, no, t);
+}
+
+void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
+ const char *origin, int no, enum grep_pat_token t)
{
struct grep_pat *p = xcalloc(1, sizeof(*p));
p->pattern = pat;
+ p->patternlen = patlen;
p->origin = origin;
p->no = no;
p->token = t;
append_header_grep_pattern(ret, pat->field,
pat->pattern);
else
- append_grep_pattern(ret, pat->pattern, pat->origin,
- pat->no, pat->token);
+ append_grep_pat(ret, pat->pattern, pat->patternlen,
+ pat->origin, pat->no, pat->token);
}
return ret;
opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
}
-static int fixmatch(const char *pattern, char *line, char *eol,
- int ignore_case, regmatch_t *match)
+static int fixmatch(struct grep_pat *p, char *line, char *eol,
+ regmatch_t *match)
{
char *hit;
- if (ignore_case) {
+ if (p->ignore_case) {
char *s = line;
do {
- hit = strcasestr(s, pattern);
+ hit = strcasestr(s, p->pattern);
if (hit)
break;
s += strlen(s) + 1;
} while (s < eol);
} else
- hit = memmem(line, eol - line, pattern, strlen(pattern));
+ hit = memmem(line, eol - line, p->pattern, p->patternlen);
if (!hit) {
match->rm_so = match->rm_eo = -1;
}
else {
match->rm_so = hit - line;
- match->rm_eo = match->rm_so + strlen(pattern);
+ match->rm_eo = match->rm_so + p->patternlen;
return 0;
}
}
again:
if (p->fixed)
- hit = !fixmatch(p->pattern, bol, eol, p->ignore_case, pmatch);
+ hit = !fixmatch(p, bol, eol, pmatch);
else
hit = !regmatch(&p->regexp, bol, eol, pmatch, eflags);
int hit;
regmatch_t m;
- if (p->fixed) {
- hit = !fixmatch(p->pattern, bol, bol + *left_p,
- p->ignore_case, &m);
- } else
+ if (p->fixed)
+ hit = !fixmatch(p, bol, bol + *left_p, &m);
+ else
hit = !regmatch(&p->regexp, bol, bol + *left_p, &m, 0);
if (!hit || m.rm_so < 0 || m.rm_eo < 0)
continue;
index 89342e5b47f6d63dd546e738e4cbf023c447b382..0aebebd96692f94877259bf64a1fece9e47d222e 100644 (file)
--- a/grep.h
+++ b/grep.h
int no;
enum grep_pat_token token;
const char *pattern;
+ size_t patternlen;
enum grep_header_field field;
regex_t regexp;
unsigned fixed:1;
void *output_priv;
};
+extern void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
extern void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
extern void compile_grep_patterns(struct grep_opt *opt);
diff --git a/t/t7008-grep-binary.sh b/t/t7008-grep-binary.sh
index 4f5e74fed77a484c4c0b7bd9de7f0b95cf714a22..eb8ca88cce59c234d8ee43cc94421024f6b1e0b5 100755 (executable)
--- a/t/t7008-grep-binary.sh
+++ b/t/t7008-grep-binary.sh
git grep .fi a
'
+test_expect_success 'git grep -F y<NUL>f a' "
+ printf 'y\000f' >f &&
+ git grep -f f -F a
+"
+
+test_expect_success 'git grep -F y<NUL>x a' "
+ printf 'y\000x' >f &&
+ test_must_fail git grep -f f -F a
+"
+
+test_expect_success 'git grep -Fi Y<NUL>f a' "
+ printf 'Y\000f' >f &&
+ git grep -f f -Fi a
+"
+
+test_expect_failure 'git grep -Fi Y<NUL>x a' "
+ printf 'Y\000x' >f &&
+ test_must_fail git grep -f f -Fi a
+"
+
+test_expect_success 'git grep y<NUL>f a' "
+ printf 'y\000f' >f &&
+ git grep -f f a
+"
+
+test_expect_failure 'git grep y<NUL>x a' "
+ printf 'y\000x' >f &&
+ test_must_fail git grep -f f a
+"
+
test_done