Skip to content

Commit b3f04e5

Browse files
committed
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
2 parents 6c3daa2 + a25b908 commit b3f04e5

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

grep.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
477477
int options = PCRE2_MULTILINE;
478478
const uint8_t *character_tables = NULL;
479479
int jitret;
480+
int patinforet;
481+
size_t jitsizearg;
480482

481483
assert(opt->pcre2);
482484

@@ -511,6 +513,30 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
511513
jitret = pcre2_jit_compile(p->pcre2_pattern, PCRE2_JIT_COMPLETE);
512514
if (jitret)
513515
die("Couldn't JIT the PCRE2 pattern '%s', got '%d'\n", p->pattern, jitret);
516+
517+
/*
518+
* The pcre2_config(PCRE2_CONFIG_JIT, ...) call just
519+
* tells us whether the library itself supports JIT,
520+
* but to see whether we're going to be actually using
521+
* JIT we need to extract PCRE2_INFO_JITSIZE from the
522+
* pattern *after* we do pcre2_jit_compile() above.
523+
*
524+
* This is because if the pattern contains the
525+
* (*NO_JIT) verb (see pcre2syntax(3))
526+
* pcre2_jit_compile() will exit early with 0. If we
527+
* then proceed to call pcre2_jit_match() further down
528+
* the line instead of pcre2_match() we'll either
529+
* segfault (pre PCRE 10.31) or run into a fatal error
530+
* (post PCRE2 10.31)
531+
*/
532+
patinforet = pcre2_pattern_info(p->pcre2_pattern, PCRE2_INFO_JITSIZE, &jitsizearg);
533+
if (patinforet)
534+
BUG("pcre2_pattern_info() failed: %d", patinforet);
535+
if (jitsizearg == 0) {
536+
p->pcre2_jit_on = 0;
537+
return;
538+
}
539+
514540
p->pcre2_jit_stack = pcre2_jit_stack_create(1, 1024 * 1024, NULL);
515541
if (!p->pcre2_jit_stack)
516542
die("Couldn't allocate PCRE2 JIT stack");

t/README

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,18 @@ use these, and "test_set_prereq" for how to define your own.
808808
Git was compiled with support for PCRE. Wrap any tests
809809
that use git-grep --perl-regexp or git-grep -P in these.
810810

811+
- LIBPCRE1
812+
813+
Git was compiled with PCRE v1 support via
814+
USE_LIBPCRE1=YesPlease. Wrap any PCRE using tests that for some
815+
reason need v1 of the PCRE library instead of v2 in these.
816+
817+
- LIBPCRE2
818+
819+
Git was compiled with PCRE v2 support via
820+
USE_LIBPCRE2=YesPlease. Wrap any PCRE using tests that for some
821+
reason need v2 of the PCRE library instead of v1 in these.
822+
811823
- CASE_INSENSITIVE_FS
812824

813825
Test is run on a case insensitive file system.

t/t7810-grep.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,12 @@ test_expect_success PCRE 'grep -P pattern' '
11311131
test_cmp expected actual
11321132
'
11331133

1134+
test_expect_success LIBPCRE2 "grep -P with (*NO_JIT) doesn't error out" '
1135+
git grep -P "(*NO_JIT)\p{Ps}.*?\p{Pe}" hello.c >actual &&
1136+
test_cmp expected actual
1137+
1138+
'
1139+
11341140
test_expect_success !PCRE 'grep -P pattern errors without PCRE' '
11351141
test_must_fail git grep -P "foo.*bar"
11361142
'

t/test-lib.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,8 @@ test -z "$NO_PERL" && test_set_prereq PERL
10281028
test -z "$NO_PTHREADS" && test_set_prereq PTHREADS
10291029
test -z "$NO_PYTHON" && test_set_prereq PYTHON
10301030
test -n "$USE_LIBPCRE1$USE_LIBPCRE2" && test_set_prereq PCRE
1031+
test -n "$USE_LIBPCRE1" && test_set_prereq LIBPCRE1
1032+
test -n "$USE_LIBPCRE2" && test_set_prereq LIBPCRE2
10311033
test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
10321034

10331035
# Can we rely on git's output in the C locale?

0 commit comments

Comments
 (0)