Skip to content

Commit 95ca1f9

Browse files
avargitster
authored andcommitted
grep/pcre2: better support invalid UTF-8 haystacks
Improve the support for invalid UTF-8 haystacks given a non-ASCII needle when using the PCREv2 backend. This is a more complete fix for a bug I started to fix in 870eea8 (grep: do not enter PCRE2_UTF mode on fixed matching, 2019-07-26), now that PCREv2 has the PCRE2_MATCH_INVALID_UTF mode we can make use of it. This fixes the sort of case described in 8a59998 (grep: stess test PCRE v2 on invalid UTF-8 data, 2019-07-26), i.e.: - The subject string is non-ASCII (e.g. "ævar") - We're under a is_utf8_locale(), e.g. "en_US.UTF-8", not "C" - We are using --ignore-case, or we're a non-fixed pattern If those conditions were satisfied and we matched found non-valid UTF-8 data PCREv2 might bark on it, in practice this only happened under the JIT backend (turned on by default on most platforms). Ultimately this fixes a "regression" in b65abca ("grep: use PCRE v2 for optimized fixed-string search", 2019-07-01), I'm putting that in scare-quotes because before then we wouldn't properly support these complex case-folding, locale etc. cases either, it just broke in different ways. There was a bug related to this the PCRE2_NO_START_OPTIMIZE flag fixed in PCREv2 10.36. It can be worked around by setting the PCRE2_NO_START_OPTIMIZE flag. Let's do that in those cases, and add tests for the bug. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a4fea08 commit 95ca1f9

File tree

7 files changed

+81
-2
lines changed

7 files changed

+81
-2
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ TEST_BUILTINS_OBJS += test-online-cpus.o
722722
TEST_BUILTINS_OBJS += test-parse-options.o
723723
TEST_BUILTINS_OBJS += test-parse-pathspec-file.o
724724
TEST_BUILTINS_OBJS += test-path-utils.o
725+
TEST_BUILTINS_OBJS += test-pcre2-config.o
725726
TEST_BUILTINS_OBJS += test-pkt-line.o
726727
TEST_BUILTINS_OBJS += test-prio-queue.o
727728
TEST_BUILTINS_OBJS += test-proc-receive.o

grep.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,23 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
492492
}
493493
if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
494494
!(!opt->ignore_case && (p->fixed || p->is_fixed)))
495-
options |= PCRE2_UTF;
495+
options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
496+
497+
/* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */
498+
if (PCRE2_MATCH_INVALID_UTF && options & (PCRE2_UTF | PCRE2_CASELESS)) {
499+
struct strbuf buf;
500+
int len;
501+
int err;
502+
503+
if ((len = pcre2_config(PCRE2_CONFIG_VERSION, NULL)) < 0)
504+
BUG("pcre2_config(..., NULL) failed: %d", len);
505+
strbuf_init(&buf, len + 1);
506+
if ((err = pcre2_config(PCRE2_CONFIG_VERSION, buf.buf)) < 0)
507+
BUG("pcre2_config(..., buf.buf) failed: %d", err);
508+
if (versioncmp(buf.buf, "10.36") < 0)
509+
options |= PCRE2_NO_START_OPTIMIZE;
510+
strbuf_release(&buf);
511+
}
496512

497513
p->pcre2_pattern = pcre2_compile((PCRE2_SPTR)p->pattern,
498514
p->patternlen, options, &error, &erroffset,

grep.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ typedef int pcre2_code;
1818
typedef int pcre2_match_data;
1919
typedef int pcre2_compile_context;
2020
#endif
21+
#ifndef PCRE2_MATCH_INVALID_UTF
22+
/* PCRE2_MATCH_* dummy also with !USE_LIBPCRE2, for test-pcre2-config.c */
23+
#define PCRE2_MATCH_INVALID_UTF 0
24+
#endif
2125
#include "thread-utils.h"
2226
#include "userdiff.h"
2327

t/helper/test-pcre2-config.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "test-tool.h"
2+
#include "cache.h"
3+
#include "grep.h"
4+
5+
int cmd__pcre2_config(int argc, const char **argv)
6+
{
7+
if (argc == 2 && !strcmp(argv[1], "has-PCRE2_MATCH_INVALID_UTF")) {
8+
int value = PCRE2_MATCH_INVALID_UTF;
9+
return !value;
10+
}
11+
return 1;
12+
}

t/helper/test-tool.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static struct test_cmd cmds[] = {
4646
{ "parse-options", cmd__parse_options },
4747
{ "parse-pathspec-file", cmd__parse_pathspec_file },
4848
{ "path-utils", cmd__path_utils },
49+
{ "pcre2-config", cmd__pcre2_config },
4950
{ "pkt-line", cmd__pkt_line },
5051
{ "prio-queue", cmd__prio_queue },
5152
{ "proc-receive", cmd__proc_receive},

t/helper/test-tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ int cmd__online_cpus(int argc, const char **argv);
3535
int cmd__parse_options(int argc, const char **argv);
3636
int cmd__parse_pathspec_file(int argc, const char** argv);
3737
int cmd__path_utils(int argc, const char **argv);
38+
int cmd__pcre2_config(int argc, const char **argv);
3839
int cmd__pkt_line(int argc, const char **argv);
3940
int cmd__prio_queue(int argc, const char **argv);
4041
int cmd__proc_receive(int argc, const char **argv);

t/t7812-grep-icase-non-ascii.sh

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: setup invalid UTF-8 data'
5757
printf "\\200\\n" >invalid-0x80 &&
5858
echo "ævar" >expected &&
5959
cat expected >>invalid-0x80 &&
60-
git add invalid-0x80
60+
git add invalid-0x80 &&
61+
62+
# Test for PCRE2_MATCH_INVALID_UTF bug
63+
# https://bugs.exim.org/show_bug.cgi?id=2642
64+
printf "\\345Aæ\\n" >invalid-0xe5 &&
65+
git add invalid-0xe5
6166
'
6267

6368
test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep ASCII from invalid UTF-8 data' '
@@ -67,16 +72,55 @@ test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep ASCII from invalid UT
6772
test_cmp expected actual
6873
'
6974

75+
test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep ASCII from invalid UTF-8 data (PCRE2 bug #2642)' '
76+
git grep -h "Aæ" invalid-0xe5 >actual &&
77+
test_cmp invalid-0xe5 actual &&
78+
git grep -h "(*NO_JIT)Aæ" invalid-0xe5 >actual &&
79+
test_cmp invalid-0xe5 actual
80+
'
81+
7082
test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invalid UTF-8 data' '
7183
git grep -h "æ" invalid-0x80 >actual &&
7284
test_cmp expected actual &&
7385
git grep -h "(*NO_JIT)æ" invalid-0x80 >actual &&
7486
test_cmp expected actual
7587
'
7688

89+
test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invalid UTF-8 data (PCRE2 bug #2642)' '
90+
git grep -h "Aæ" invalid-0xe5 >actual &&
91+
test_cmp invalid-0xe5 actual &&
92+
git grep -h "(*NO_JIT)Aæ" invalid-0xe5 >actual &&
93+
test_cmp invalid-0xe5 actual
94+
'
95+
96+
test_lazy_prereq PCRE2_MATCH_INVALID_UTF '
97+
test-tool pcre2-config has-PCRE2_MATCH_INVALID_UTF
98+
'
99+
77100
test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invalid UTF-8 data with -i' '
78101
test_might_fail git grep -hi "Æ" invalid-0x80 >actual &&
79102
test_might_fail git grep -hi "(*NO_JIT)Æ" invalid-0x80 >actual
80103
'
81104

105+
test_expect_success GETTEXT_LOCALE,LIBPCRE2,PCRE2_MATCH_INVALID_UTF 'PCRE v2: grep non-ASCII from invalid UTF-8 data with -i' '
106+
git grep -hi "Æ" invalid-0x80 >actual &&
107+
test_cmp expected actual &&
108+
git grep -hi "(*NO_JIT)Æ" invalid-0x80 >actual &&
109+
test_cmp expected actual
110+
'
111+
112+
test_expect_success GETTEXT_LOCALE,LIBPCRE2,PCRE2_MATCH_INVALID_UTF 'PCRE v2: grep non-ASCII from invalid UTF-8 data with -i (PCRE2 bug #2642)' '
113+
git grep -hi "Æ" invalid-0xe5 >actual &&
114+
test_cmp invalid-0xe5 actual &&
115+
git grep -hi "(*NO_JIT)Æ" invalid-0xe5 >actual &&
116+
test_cmp invalid-0xe5 actual &&
117+
118+
# Only the case of grepping the ASCII part in a way that
119+
# relies on -i fails
120+
git grep -hi "aÆ" invalid-0xe5 >actual &&
121+
test_cmp invalid-0xe5 actual &&
122+
git grep -hi "(*NO_JIT)aÆ" invalid-0xe5 >actual &&
123+
test_cmp invalid-0xe5 actual
124+
'
125+
82126
test_done

0 commit comments

Comments
 (0)