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 870eea8166 (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 8a5999838e (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 b65abcafc7 ("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 <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jan 24, 2021 at 18:28 UTC 95ca1f987edd23389e3079d0a7fe6d0f89927b68
7 files changed +81 -2
Makefile
+1
@@ -722,6 +722,7 @@ TEST_BUILTINS_OBJS += test-online-cpus.o
722 TEST_BUILTINS_OBJS += test-parse-options.o
723 TEST_BUILTINS_OBJS += test-parse-pathspec-file.o
724 TEST_BUILTINS_OBJS += test-path-utils.o
725 +TEST_BUILTINS_OBJS += test-pcre2-config.o
726 TEST_BUILTINS_OBJS += test-pkt-line.o
727 TEST_BUILTINS_OBJS += test-prio-queue.o
728 TEST_BUILTINS_OBJS += test-proc-receive.o
grep.c
+17 -1
@@ -492,7 +492,23 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
492 }
493 if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
494 !(!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 + }
512
513 p->pcre2_pattern = pcre2_compile((PCRE2_SPTR)p->pattern,
514 p->patternlen, options, &error, &erroffset,
grep.h
+4
@@ -18,6 +18,10 @@ typedef int pcre2_code;
18 typedef int pcre2_match_data;
19 typedef int pcre2_compile_context;
20 #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
25 #include "thread-utils.h"
26 #include "userdiff.h"
27
t/helper/test-pcre2-config.c new
+12
@@ -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
+1
@@ -46,6 +46,7 @@ static struct test_cmd cmds[] = {
46 { "parse-options", cmd__parse_options },
47 { "parse-pathspec-file", cmd__parse_pathspec_file },
48 { "path-utils", cmd__path_utils },
49 + { "pcre2-config", cmd__pcre2_config },
50 { "pkt-line", cmd__pkt_line },
51 { "prio-queue", cmd__prio_queue },
52 { "proc-receive", cmd__proc_receive},
t/helper/test-tool.h
+1
@@ -35,6 +35,7 @@ int cmd__online_cpus(int argc, const char **argv);
35 int cmd__parse_options(int argc, const char **argv);
36 int cmd__parse_pathspec_file(int argc, const char** argv);
37 int cmd__path_utils(int argc, const char **argv);
38 +int cmd__pcre2_config(int argc, const char **argv);
39 int cmd__pkt_line(int argc, const char **argv);
40 int cmd__prio_queue(int argc, const char **argv);
41 int cmd__proc_receive(int argc, const char **argv);
t/t7812-grep-icase-non-ascii.sh
+45 -1
@@ -57,7 +57,12 @@ test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: setup invalid UTF-8 data'
57 printf "\\200\\n" >invalid-0x80 &&
58 echo "ævar" >expected &&
59 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
66 '
67
68 test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep ASCII from invalid UTF-8 data' '
@@ -67,6 +72,13 @@ test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep ASCII from invalid UT
72 test_cmp expected actual
73 '
74
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 +
82 test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invalid UTF-8 data' '
83 git grep -h "æ" invalid-0x80 >actual &&
84 test_cmp expected actual &&
@@ -74,9 +86,41 @@ test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invali
86 test_cmp expected actual
87 '
88
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 +
100 test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invalid UTF-8 data with -i' '
101 test_might_fail git grep -hi "Æ" invalid-0x80 >actual &&
102 test_might_fail git grep -hi "(*NO_JIT)Æ" invalid-0x80 >actual
103 '
104
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 +
126 test_done