Skip to content

Commit

Permalink
Apply limit of 65535 to the number of capturing pairs in a match data…
Browse files Browse the repository at this point in the history
… block (GitHub #176)
  • Loading branch information
PhilipHazel committed Dec 12, 2022
1 parent d598609 commit fb23bb1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ the allowed maximum, the error message displayed the hard limit incorrectly.
This was pointed out on GitHub pull request #171, but the suggested patch
didn't cope with all cases. Some further modification was required.

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


Version 10.41 06-December-2022
------------------------------
Expand Down
4 changes: 3 additions & 1 deletion doc/pcre2api.3
Original file line number Diff line number Diff line change
Expand Up @@ -2519,7 +2519,9 @@ large enough to hold as many as are expected.
A minimum of at least 1 pair is imposed by \fBpcre2_match_data_create()\fP, so
it is always possible to return the overall matched string in the case of
\fBpcre2_match()\fP or the longest match in the case of
\fBpcre2_dfa_match()\fP.
\fBpcre2_dfa_match()\fP. The maximum number of pairs is 65535; if the the first
argument of \fBpcre2_match_data_create()\fP is greater than this, 65535 is
used.
.P
The second argument of \fBpcre2_match_data_create()\fP is a pointer to a
general context, which can specify custom memory management for obtaining the
Expand Down
4 changes: 3 additions & 1 deletion src/pcre2_match_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ POSSIBILITY OF SUCH DAMAGE.
* Create a match data block given ovector size *
*************************************************/

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

PCRE2_EXP_DEFN pcre2_match_data * PCRE2_CALL_CONVENTION
pcre2_match_data_create(uint32_t oveccount, pcre2_general_context *gcontext)
{
pcre2_match_data *yield;
if (oveccount < 1) oveccount = 1;
if (oveccount > UINT16_MAX) oveccount = UINT16_MAX;
yield = PRIV(memctl_malloc)(
offsetof(pcre2_match_data, ovector) + 2*oveccount*sizeof(PCRE2_SIZE),
(pcre2_memctl *)gcontext);
Expand Down
5 changes: 5 additions & 0 deletions testdata/testinput2
Original file line number Diff line number Diff line change
Expand Up @@ -5934,5 +5934,10 @@ a)"xI

--
\[X]{-10}

# Check imposition of maximum by match_data_create().

/abcd/
abcd\=ovector=65536

# End of testinput2
6 changes: 6 additions & 0 deletions testdata/testoutput2
Original file line number Diff line number Diff line change
Expand Up @@ -17749,6 +17749,12 @@ Subject length lower bound = 2
--
\[X]{-10}
** Zero or negative repeat not allowed

# Check imposition of maximum by match_data_create().

/abcd/
abcd\=ovector=65536
0: abcd

# End of testinput2
Error -70: PCRE2_ERROR_BADDATA (unknown error number)
Expand Down

0 comments on commit fb23bb1

Please sign in to comment.