Skip to content

Commit 8fb4451

Browse files
committed
pcre2_match_data: allow pcre2_match_data_create_from_pattern(NULL,)
Prevents crashing in that case, and matches how other API calls handle bad parameters. It is also useful to simplify error checking in the caller side, as it allows delaying checks for compiler failure and handling it together with a match data creation failure.
1 parent 7c7a274 commit 8fb4451

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

doc/pcre2_match_data_create_from_pattern.3

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ PCRE2 - Perl-compatible regular expressions (revised API)
1515
.rs
1616
.sp
1717
This function creates a new match data block for holding the result of a match.
18-
The first argument points to a compiled pattern. The number of capturing
19-
parentheses within the pattern is used to compute the number of pairs of
20-
offsets that are required in the match data block. These form the "output
21-
vector" (ovector) within the match data block, and are used to identify the
22-
matched string and any captured substrings when matching with
23-
\fBpcre2_match()\fP. If you are using \fBpcre2_dfa_match()\fP, which uses the
24-
output vector in a different way, you should use \fBpcre2_match_data_create()\fP
25-
instead of this function.
18+
If the first argument is NULL, this function returns NULL, otherwise the first
19+
argument points to a compiled pattern. The number of capturing parentheses
20+
within the pattern is used to compute the number of pairs of offsets that are
21+
required in the match data block. These form the "output vector" (ovector)
22+
within the match data block, and are used to identify the matched string and
23+
any captured substrings when matching with \fBpcre2_match()\fP. If you are
24+
using \fBpcre2_dfa_match()\fP, which uses the output vector in a different way,
25+
you should use \fBpcre2_match_data_create()\fP instead of this function.
2626
.P
2727
The second argument points to a general context, for custom memory management,
28-
or is NULL to use the same memory allocator as was used for the compiled
28+
or is NULL to use the same memory allocator that was used for the compiled
2929
pattern. The result of the function is NULL if the memory for the block could
30-
not be obtained.
30+
not be obtained or if NULL was provided as the first argument.
3131
.P
3232
There is a complete description of the PCRE2 native API in the
3333
.\" HREF

src/pcre2_match_data.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ PCRE2_EXP_DEFN pcre2_match_data * PCRE2_CALL_CONVENTION
8181
pcre2_match_data_create_from_pattern(const pcre2_code *code,
8282
pcre2_general_context *gcontext)
8383
{
84+
if (code == NULL) return NULL;
8485
if (gcontext == NULL) gcontext = (pcre2_general_context *)code;
8586
return pcre2_match_data_create(((const pcre2_real_code *)code)->top_bracket + 1,
8687
gcontext);

0 commit comments

Comments
 (0)