grep: support NUL chars in search strings for -F
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>
René Scharfe committed
May 22, 2010 at 23:43 UTC
ed40a0951cedb70777669144478166aa5bb2cf9c
4 files changed
+58
-15
builtin/grep.c
+6
-2
@@ -724,11 +724,15 @@ static int file_callback(const struct option *opt, const char *arg, int unset)
724
if (!patterns)
725
die_errno("cannot open '%s'", arg);
726
while (strbuf_getline(&sb, patterns, '\n') == 0) {
727
+ char *s;
728
+ size_t len;
729
+
730
/* ignore empty line like grep does */
731
if (sb.len == 0)
732
continue;
730
- append_grep_pattern(grep_opt, strbuf_detach(&sb, NULL), arg,
731
- ++lno, GREP_PATTERN);
733
+
734
+ s = strbuf_detach(&sb, &len);
735
+ append_grep_pat(grep_opt, s, len, arg, ++lno, GREP_PATTERN);
736
}
737
fclose(patterns);
738
strbuf_release(&sb);
grep.c
+20
-13
@@ -7,6 +7,7 @@ void append_header_grep_pattern(struct grep_opt *opt, enum grep_header_field fie
7
{
8
struct grep_pat *p = xcalloc(1, sizeof(*p));
9
p->pattern = pat;
10
+ p->patternlen = strlen(pat);
11
p->origin = "header";
12
p->no = 0;
13
p->token = GREP_PATTERN_HEAD;
@@ -18,9 +19,16 @@ void append_header_grep_pattern(struct grep_opt *opt, enum grep_header_field fie
19
20
void append_grep_pattern(struct grep_opt *opt, const char *pat,
21
const char *origin, int no, enum grep_pat_token t)
22
+{
23
+ append_grep_pat(opt, pat, strlen(pat), origin, no, t);
24
+}
25
+
26
+void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
27
+ const char *origin, int no, enum grep_pat_token t)
28
{
29
struct grep_pat *p = xcalloc(1, sizeof(*p));
30
p->pattern = pat;
31
+ p->patternlen = patlen;
32
p->origin = origin;
33
p->no = no;
34
p->token = t;
@@ -44,8 +52,8 @@ struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
52
append_header_grep_pattern(ret, pat->field,
53
pat->pattern);
54
else
47
- append_grep_pattern(ret, pat->pattern, pat->origin,
48
- pat->no, pat->token);
55
+ append_grep_pat(ret, pat->pattern, pat->patternlen,
56
+ pat->origin, pat->no, pat->token);
57
}
58
59
return ret;
@@ -329,21 +337,21 @@ static void show_name(struct grep_opt *opt, const char *name)
337
opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
338
}
339
332
-static int fixmatch(const char *pattern, char *line, char *eol,
333
- int ignore_case, regmatch_t *match)
340
+static int fixmatch(struct grep_pat *p, char *line, char *eol,
341
+ regmatch_t *match)
342
{
343
char *hit;
344
337
- if (ignore_case) {
345
+ if (p->ignore_case) {
346
char *s = line;
347
do {
340
- hit = strcasestr(s, pattern);
348
+ hit = strcasestr(s, p->pattern);
349
if (hit)
350
break;
351
s += strlen(s) + 1;
352
} while (s < eol);
353
} else
346
- hit = memmem(line, eol - line, pattern, strlen(pattern));
354
+ hit = memmem(line, eol - line, p->pattern, p->patternlen);
355
356
if (!hit) {
357
match->rm_so = match->rm_eo = -1;
@@ -351,7 +359,7 @@ static int fixmatch(const char *pattern, char *line, char *eol,
359
}
360
else {
361
match->rm_so = hit - line;
354
- match->rm_eo = match->rm_so + strlen(pattern);
362
+ match->rm_eo = match->rm_so + p->patternlen;
363
return 0;
364
}
365
}
@@ -417,7 +425,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
425
426
again:
427
if (p->fixed)
420
- hit = !fixmatch(p->pattern, bol, eol, p->ignore_case, pmatch);
428
+ hit = !fixmatch(p, bol, eol, pmatch);
429
else
430
hit = !regmatch(&p->regexp, bol, eol, pmatch, eflags);
431
@@ -743,10 +751,9 @@ static int look_ahead(struct grep_opt *opt,
751
int hit;
752
regmatch_t m;
753
746
- if (p->fixed) {
747
- hit = !fixmatch(p->pattern, bol, bol + *left_p,
748
- p->ignore_case, &m);
749
- } else
754
+ if (p->fixed)
755
+ hit = !fixmatch(p, bol, bol + *left_p, &m);
756
+ else
757
hit = !regmatch(&p->regexp, bol, bol + *left_p, &m, 0);
758
if (!hit || m.rm_so < 0 || m.rm_eo < 0)
759
continue;
grep.h
+2
@@ -29,6 +29,7 @@ struct grep_pat {
29
int no;
30
enum grep_pat_token token;
31
const char *pattern;
32
+ size_t patternlen;
33
enum grep_header_field field;
34
regex_t regexp;
35
unsigned fixed:1;
@@ -104,6 +105,7 @@ struct grep_opt {
105
void *output_priv;
106
};
107
108
+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);
109
extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
110
extern void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
111
extern void compile_grep_patterns(struct grep_opt *opt);
t/t7008-grep-binary.sh
+30
@@ -69,4 +69,34 @@ test_expect_failure 'git grep .fi a' '
69
git grep .fi a
70
'
71
72
+test_expect_success 'git grep -F y<NUL>f a' "
73
+ printf 'y\000f' >f &&
74
+ git grep -f f -F a
75
+"
76
+
77
+test_expect_success 'git grep -F y<NUL>x a' "
78
+ printf 'y\000x' >f &&
79
+ test_must_fail git grep -f f -F a
80
+"
81
+
82
+test_expect_success 'git grep -Fi Y<NUL>f a' "
83
+ printf 'Y\000f' >f &&
84
+ git grep -f f -Fi a
85
+"
86
+
87
+test_expect_failure 'git grep -Fi Y<NUL>x a' "
88
+ printf 'Y\000x' >f &&
89
+ test_must_fail git grep -f f -Fi a
90
+"
91
+
92
+test_expect_success 'git grep y<NUL>f a' "
93
+ printf 'y\000f' >f &&
94
+ git grep -f f a
95
+"
96
+
97
+test_expect_failure 'git grep y<NUL>x a' "
98
+ printf 'y\000x' >f &&
99
+ test_must_fail git grep -f f a
100
+"
101
+
102
test_done