Merge branch 'ab/pcre2-grep'

"git grep" compiled with libpcre2 sometimes triggered a segfault,
which is being fixed.

* ab/pcre2-grep:
  grep: fix segfault under -P + PCRE2 <=10.30 + (*NO_JIT)
  test-lib: add LIBPCRE1 & LIBPCRE2 prerequisites
This commit is contained in:
Junio C Hamano 2017-12-13 13:28:54 -08:00
commit b3f04e5b4c
4 changed files with 46 additions and 0 deletions

26
grep.c
View file

@ -477,6 +477,8 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
int options = PCRE2_MULTILINE;
const uint8_t *character_tables = NULL;
int jitret;
int patinforet;
size_t jitsizearg;
assert(opt->pcre2);
@ -511,6 +513,30 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
jitret = pcre2_jit_compile(p->pcre2_pattern, PCRE2_JIT_COMPLETE);
if (jitret)
die("Couldn't JIT the PCRE2 pattern '%s', got '%d'\n", p->pattern, jitret);
/*
* The pcre2_config(PCRE2_CONFIG_JIT, ...) call just
* tells us whether the library itself supports JIT,
* but to see whether we're going to be actually using
* JIT we need to extract PCRE2_INFO_JITSIZE from the
* pattern *after* we do pcre2_jit_compile() above.
*
* This is because if the pattern contains the
* (*NO_JIT) verb (see pcre2syntax(3))
* pcre2_jit_compile() will exit early with 0. If we
* then proceed to call pcre2_jit_match() further down
* the line instead of pcre2_match() we'll either
* segfault (pre PCRE 10.31) or run into a fatal error
* (post PCRE2 10.31)
*/
patinforet = pcre2_pattern_info(p->pcre2_pattern, PCRE2_INFO_JITSIZE, &jitsizearg);
if (patinforet)
BUG("pcre2_pattern_info() failed: %d", patinforet);
if (jitsizearg == 0) {
p->pcre2_jit_on = 0;
return;
}
p->pcre2_jit_stack = pcre2_jit_stack_create(1, 1024 * 1024, NULL);
if (!p->pcre2_jit_stack)
die("Couldn't allocate PCRE2 JIT stack");

View file

@ -808,6 +808,18 @@ use these, and "test_set_prereq" for how to define your own.
Git was compiled with support for PCRE. Wrap any tests
that use git-grep --perl-regexp or git-grep -P in these.
- LIBPCRE1
Git was compiled with PCRE v1 support via
USE_LIBPCRE1=YesPlease. Wrap any PCRE using tests that for some
reason need v1 of the PCRE library instead of v2 in these.
- LIBPCRE2
Git was compiled with PCRE v2 support via
USE_LIBPCRE2=YesPlease. Wrap any PCRE using tests that for some
reason need v2 of the PCRE library instead of v1 in these.
- CASE_INSENSITIVE_FS
Test is run on a case insensitive file system.

View file

@ -1131,6 +1131,12 @@ test_expect_success PCRE 'grep -P pattern' '
test_cmp expected actual
'
test_expect_success LIBPCRE2 "grep -P with (*NO_JIT) doesn't error out" '
git grep -P "(*NO_JIT)\p{Ps}.*?\p{Pe}" hello.c >actual &&
test_cmp expected actual
'
test_expect_success !PCRE 'grep -P pattern errors without PCRE' '
test_must_fail git grep -P "foo.*bar"
'

View file

@ -1028,6 +1028,8 @@ test -z "$NO_PERL" && test_set_prereq PERL
test -z "$NO_PTHREADS" && test_set_prereq PTHREADS
test -z "$NO_PYTHON" && test_set_prereq PYTHON
test -n "$USE_LIBPCRE1$USE_LIBPCRE2" && test_set_prereq PCRE
test -n "$USE_LIBPCRE1" && test_set_prereq LIBPCRE1
test -n "$USE_LIBPCRE2" && test_set_prereq LIBPCRE2
test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
# Can we rely on git's output in the C locale?