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
This commit is contained in:
Junio C Hamano 2021-02-10 14:48:33 -08:00
Родитель 0199c68d01 95ca1f987e
Коммит 59ace284f3
7 изменённых файлов: 82 добавлений и 8 удалений

Просмотреть файл

@ -715,6 +715,7 @@ TEST_BUILTINS_OBJS += test-online-cpus.o
TEST_BUILTINS_OBJS += test-parse-options.o
TEST_BUILTINS_OBJS += test-parse-pathspec-file.o
TEST_BUILTINS_OBJS += test-path-utils.o
TEST_BUILTINS_OBJS += test-pcre2-config.o
TEST_BUILTINS_OBJS += test-pkt-line.o
TEST_BUILTINS_OBJS += test-prio-queue.o
TEST_BUILTINS_OBJS += test-proc-receive.o

18
grep.c
Просмотреть файл

@ -391,7 +391,23 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
}
if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
!(!opt->ignore_case && (p->fixed || p->is_fixed)))
options |= PCRE2_UTF;
options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
/* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */
if (PCRE2_MATCH_INVALID_UTF && options & (PCRE2_UTF | PCRE2_CASELESS)) {
struct strbuf buf;
int len;
int err;
if ((len = pcre2_config(PCRE2_CONFIG_VERSION, NULL)) < 0)
BUG("pcre2_config(..., NULL) failed: %d", len);
strbuf_init(&buf, len + 1);
if ((err = pcre2_config(PCRE2_CONFIG_VERSION, buf.buf)) < 0)
BUG("pcre2_config(..., buf.buf) failed: %d", err);
if (versioncmp(buf.buf, "10.36") < 0)
options |= PCRE2_NO_START_OPTIMIZE;
strbuf_release(&buf);
}
p->pcre2_pattern = pcre2_compile((PCRE2_SPTR)p->pattern,
p->patternlen, options, &error, &erroffset,

4
grep.h
Просмотреть файл

@ -9,6 +9,10 @@ typedef int pcre2_code;
typedef int pcre2_match_data;
typedef int pcre2_compile_context;
#endif
#ifndef PCRE2_MATCH_INVALID_UTF
/* PCRE2_MATCH_* dummy also with !USE_LIBPCRE2, for test-pcre2-config.c */
#define PCRE2_MATCH_INVALID_UTF 0
#endif
#include "thread-utils.h"
#include "userdiff.h"

Просмотреть файл

@ -0,0 +1,12 @@
#include "test-tool.h"
#include "cache.h"
#include "grep.h"
int cmd__pcre2_config(int argc, const char **argv)
{
if (argc == 2 && !strcmp(argv[1], "has-PCRE2_MATCH_INVALID_UTF")) {
int value = PCRE2_MATCH_INVALID_UTF;
return !value;
}
return 1;
}

Просмотреть файл

@ -46,6 +46,7 @@ static struct test_cmd cmds[] = {
{ "parse-options", cmd__parse_options },
{ "parse-pathspec-file", cmd__parse_pathspec_file },
{ "path-utils", cmd__path_utils },
{ "pcre2-config", cmd__pcre2_config },
{ "pkt-line", cmd__pkt_line },
{ "prio-queue", cmd__prio_queue },
{ "proc-receive", cmd__proc_receive},

Просмотреть файл

@ -35,6 +35,7 @@ int cmd__online_cpus(int argc, const char **argv);
int cmd__parse_options(int argc, const char **argv);
int cmd__parse_pathspec_file(int argc, const char** argv);
int cmd__path_utils(int argc, const char **argv);
int cmd__pcre2_config(int argc, const char **argv);
int cmd__pkt_line(int argc, const char **argv);
int cmd__prio_queue(int argc, const char **argv);
int cmd__proc_receive(int argc, const char **argv);

Просмотреть файл

@ -57,7 +57,12 @@ test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: setup invalid UTF-8 data'
printf "\\200\\n" >invalid-0x80 &&
echo "ævar" >expected &&
cat expected >>invalid-0x80 &&
git add invalid-0x80
git add invalid-0x80 &&
# Test for PCRE2_MATCH_INVALID_UTF bug
# https://bugs.exim.org/show_bug.cgi?id=2642
printf "\\345Aæ\\n" >invalid-0xe5 &&
git add invalid-0xe5
'
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
test_cmp expected actual
'
test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep ASCII from invalid UTF-8 data (PCRE2 bug #2642)' '
git grep -h "Aæ" invalid-0xe5 >actual &&
test_cmp invalid-0xe5 actual &&
git grep -h "(*NO_JIT)Aæ" invalid-0xe5 >actual &&
test_cmp invalid-0xe5 actual
'
test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invalid UTF-8 data' '
git grep -h "æ" invalid-0x80 >actual &&
test_cmp expected actual &&
@ -74,14 +86,41 @@ test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invali
test_cmp expected actual
'
test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invalid UTF-8 data (PCRE2 bug #2642)' '
git grep -h "Aæ" invalid-0xe5 >actual &&
test_cmp invalid-0xe5 actual &&
git grep -h "(*NO_JIT)Aæ" invalid-0xe5 >actual &&
test_cmp invalid-0xe5 actual
'
test_lazy_prereq PCRE2_MATCH_INVALID_UTF '
test-tool pcre2-config has-PCRE2_MATCH_INVALID_UTF
'
test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invalid UTF-8 data with -i' '
test_might_fail git grep -hi "Æ" invalid-0x80 >actual &&
if test -s actual
then
test_cmp expected actual
fi &&
test_must_fail git grep -hi "(*NO_JIT)Æ" invalid-0x80 >actual &&
! test_cmp expected actual
test_might_fail git grep -hi "(*NO_JIT)Æ" invalid-0x80 >actual
'
test_expect_success GETTEXT_LOCALE,LIBPCRE2,PCRE2_MATCH_INVALID_UTF 'PCRE v2: grep non-ASCII from invalid UTF-8 data with -i' '
git grep -hi "Æ" invalid-0x80 >actual &&
test_cmp expected actual &&
git grep -hi "(*NO_JIT)Æ" invalid-0x80 >actual &&
test_cmp expected actual
'
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)' '
git grep -hi "Æ" invalid-0xe5 >actual &&
test_cmp invalid-0xe5 actual &&
git grep -hi "(*NO_JIT)Æ" invalid-0xe5 >actual &&
test_cmp invalid-0xe5 actual &&
# Only the case of grepping the ASCII part in a way that
# relies on -i fails
git grep -hi "aÆ" invalid-0xe5 >actual &&
test_cmp invalid-0xe5 actual &&
git grep -hi "(*NO_JIT)aÆ" invalid-0xe5 >actual &&
test_cmp invalid-0xe5 actual
'
test_done