Skip to content

Commit 83806f6

Browse files
committed
pcre2test: harden subject overread testing
Mark the character after the subject as not accessible if there is no NUL termination to better catch buffer overreads. Add an assert and test to avoid regressions on a recent fix, and correct the return value for non partial matches of back references.
1 parent 0c5b26f commit 83806f6

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

src/pcre2_match.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ if (offset >= Foffset_top || Fovector[offset] == PCRE2_UNSET)
387387
eptr = eptr_start = Feptr;
388388
p = mb->start_subject + Fovector[offset];
389389
length = Fovector[offset+1] - Fovector[offset];
390+
PCRE2_ASSERT(eptr <= mb->end_subject);
390391

391392
if (caseless)
392393
{
@@ -485,8 +486,8 @@ else
485486

486487
else
487488
{
488-
if ((PCRE2_SIZE)(mb->end_subject - eptr) < length) return 1; /* Partial */
489-
if (memcmp(p, eptr, CU2BYTES(length)) != 0) return -1; /* No match */
489+
if ((PCRE2_SIZE)(mb->end_subject - eptr) < length ||
490+
memcmp(p, eptr, CU2BYTES(length)) != 0) return -1; /* No match */
490491
eptr += length;
491492
}
492493
}

src/pcre2test.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8478,11 +8478,13 @@ the unused start of the buffer unaddressable. If we are using the POSIX
84788478
interface, or testing zero-termination, we must include the terminating zero in
84798479
the usable data. */
84808480

8481-
c = code_unit_size * (((pat_patctl.control & CTL_POSIX) +
8482-
(dat_datctl.control & CTL_ZERO_TERMINATE) != 0)? 1:0);
8483-
pp = memmove(dbuffer + dbuffer_size - len - c, dbuffer, len + c);
8481+
c = code_unit_size * ((((pat_patctl.control & CTL_POSIX) != 0) +
8482+
((dat_datctl.control & CTL_ZERO_TERMINATE) != 0))? 1 : 0);
8483+
pp = memmove(dbuffer + dbuffer_size - len - code_unit_size, dbuffer, len + c);
84848484
#ifdef SUPPORT_VALGRIND
8485-
VALGRIND_MAKE_MEM_NOACCESS(dbuffer, dbuffer_size - (len + c));
8485+
VALGRIND_MAKE_MEM_NOACCESS(dbuffer, dbuffer_size - (len + code_unit_size));
8486+
if (c == 0)
8487+
VALGRIND_MAKE_MEM_NOACCESS(dbuffer + dbuffer_size - code_unit_size, code_unit_size);
84868488
#endif
84878489

84888490
#if defined(EBCDIC) && !EBCDIC_IO

testdata/testinput2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6756,6 +6756,9 @@ a)"xI
67566756
/(a)(*scs:(1)a(*ACCEPT))bbb/
67576757
abbb
67586758

6759+
/(a)(b+)(*scs:(1)a(*ACCEPT))(\2)/
6760+
abbb
6761+
67596762
# Duplicated capture references
67606763

67616764
/(a)(b)(c)(d)(*scs:(4,3,1,2,2,1,3,3,4,4)x)/B

testdata/testoutput2

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20263,6 +20263,13 @@ No match
2026320263
0: abbb
2026420264
1: a
2026520265

20266+
/(a)(b+)(*scs:(1)a(*ACCEPT))(\2)/
20267+
abbb
20268+
0: abb
20269+
1: a
20270+
2: b
20271+
3: b
20272+
2026620273
# Duplicated capture references
2026720274

2026820275
/(a)(b)(c)(d)(*scs:(4,3,1,2,2,1,3,3,4,4)x)/B

0 commit comments

Comments
 (0)