Skip to content

Commit 59ace28

Browse files
committed
Merge branch 'ab/grep-pcre-invalid-utf8'
Update support for invalid UTF-8 in PCRE2. * ab/grep-pcre-invalid-utf8: grep/pcre2: better support invalid UTF-8 haystacks grep/pcre2 tests: don't rely on invalid UTF-8 data test
2 parents 0199c68 + 95ca1f9 commit 59ace28

File tree

7 files changed

+82
-8
lines changed

7 files changed

+82
-8
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ TEST_BUILTINS_OBJS += test-online-cpus.o
715715
TEST_BUILTINS_OBJS += test-parse-options.o
716716
TEST_BUILTINS_OBJS += test-parse-pathspec-file.o
717717
TEST_BUILTINS_OBJS += test-path-utils.o
718+
TEST_BUILTINS_OBJS += test-pcre2-config.o
718719
TEST_BUILTINS_OBJS += test-pkt-line.o
719720
TEST_BUILTINS_OBJS += test-prio-queue.o
720721
TEST_BUILTINS_OBJS += test-proc-receive.o

grep.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,23 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
391391
}
392392
if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
393393
!(!opt->ignore_case && (p->fixed || p->is_fixed)))
394-
options |= PCRE2_UTF;
394+
options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
395+
396+
/* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */
397+
if (PCRE2_MATCH_INVALID_UTF && options & (PCRE2_UTF | PCRE2_CASELESS)) {
398+
struct strbuf buf;
399+
int len;
400+
int err;
401+
402+
if ((len = pcre2_config(PCRE2_CONFIG_VERSION, NULL)) < 0)
403+
BUG("pcre2_config(..., NULL) failed: %d", len);
404+
strbuf_init(&buf, len + 1);
405+
if ((err = pcre2_config(PCRE2_CONFIG_VERSION, buf.buf)) < 0)
406+
BUG("pcre2_config(..., buf.buf) failed: %d", err);
407+
if (versioncmp(buf.buf, "10.36") < 0)
408+
options |= PCRE2_NO_START_OPTIMIZE;
409+
strbuf_release(&buf);
410+
}
395411

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

grep.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ typedef int pcre2_code;
99
typedef int pcre2_match_data;
1010
typedef int pcre2_compile_context;
1111
#endif
12+
#ifndef PCRE2_MATCH_INVALID_UTF
13+
/* PCRE2_MATCH_* dummy also with !USE_LIBPCRE2, for test-pcre2-config.c */
14+
#define PCRE2_MATCH_INVALID_UTF 0
15+
#endif
1216
#include "thread-utils.h"
1317
#include "userdiff.h"
1418

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: 46 additions & 7 deletions
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,21 +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 &&
79-
if test -s actual
80-
then
81-
test_cmp expected actual
82-
fi &&
83-
test_must_fail git grep -hi "(*NO_JIT)Æ" invalid-0x80 >actual &&
84-
! test_cmp expected 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
85124
'
86125

87126
test_done

0 commit comments

Comments
 (0)