Skip to content

Commit fb23bb1

Browse files
committed
Apply limit of 65535 to the number of capturing pairs in a match data block (GitHub #176)
1 parent d598609 commit fb23bb1

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ the allowed maximum, the error message displayed the hard limit incorrectly.
1818
This was pointed out on GitHub pull request #171, but the suggested patch
1919
didn't cope with all cases. Some further modification was required.
2020

21+
4. Supplying an ovector count of more than 65535 to pcre2_match_data_create()
22+
caused a crash because the field in the match data block is only 16 bits. A
23+
maximum of 65535 is now silently applied.
24+
2125

2226
Version 10.41 06-December-2022
2327
------------------------------

doc/pcre2api.3

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2519,7 +2519,9 @@ large enough to hold as many as are expected.
25192519
A minimum of at least 1 pair is imposed by \fBpcre2_match_data_create()\fP, so
25202520
it is always possible to return the overall matched string in the case of
25212521
\fBpcre2_match()\fP or the longest match in the case of
2522-
\fBpcre2_dfa_match()\fP.
2522+
\fBpcre2_dfa_match()\fP. The maximum number of pairs is 65535; if the the first
2523+
argument of \fBpcre2_match_data_create()\fP is greater than this, 65535 is
2524+
used.
25232525
.P
25242526
The second argument of \fBpcre2_match_data_create()\fP is a pointer to a
25252527
general context, which can specify custom memory management for obtaining the

src/pcre2_match_data.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ POSSIBILITY OF SUCH DAMAGE.
5151
* Create a match data block given ovector size *
5252
*************************************************/
5353

54-
/* A minimum of 1 is imposed on the number of ovector pairs. */
54+
/* A minimum of 1 is imposed on the number of ovector pairs. A maximum is also
55+
imposed because the oveccount field in a match data block is uintt6_t. */
5556

5657
PCRE2_EXP_DEFN pcre2_match_data * PCRE2_CALL_CONVENTION
5758
pcre2_match_data_create(uint32_t oveccount, pcre2_general_context *gcontext)
5859
{
5960
pcre2_match_data *yield;
6061
if (oveccount < 1) oveccount = 1;
62+
if (oveccount > UINT16_MAX) oveccount = UINT16_MAX;
6163
yield = PRIV(memctl_malloc)(
6264
offsetof(pcre2_match_data, ovector) + 2*oveccount*sizeof(PCRE2_SIZE),
6365
(pcre2_memctl *)gcontext);

testdata/testinput2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5934,5 +5934,10 @@ a)"xI
59345934

59355935
--
59365936
\[X]{-10}
5937+
5938+
# Check imposition of maximum by match_data_create().
5939+
5940+
/abcd/
5941+
abcd\=ovector=65536
59375942

59385943
# End of testinput2

testdata/testoutput2

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17749,6 +17749,12 @@ Subject length lower bound = 2
1774917749
--
1775017750
\[X]{-10}
1775117751
** Zero or negative repeat not allowed
17752+
17753+
# Check imposition of maximum by match_data_create().
17754+
17755+
/abcd/
17756+
abcd\=ovector=65536
17757+
0: abcd
1775217758

1775317759
# End of testinput2
1775417760
Error -70: PCRE2_ERROR_BADDATA (unknown error number)

0 commit comments

Comments
 (0)