pcre2_compile: make errorcode parameter optional

When the pcre2_compile interface was introduced, code was added to check
that BOTH the errorcode and erroroffset were not NULL, but usually only
one or the other is needed, in adition to a NULL return value to detect
failures.

Make the minimum change possible to allow passing a NULL and simplify
some of the calls used in pcre2_jit_test that will benefit from this
change.

A more complex change that would also avoid the multiple places where
checks for it were added has been punted from this change.
This commit is contained in:
Carlo Marcelo Arenas Belón 2022-04-18 14:29:58 -07:00
parent 3103b8f20a
commit 7e363f5dda
3 changed files with 16 additions and 16 deletions

View File

@ -21,7 +21,7 @@ arguments are:
\fIpattern\fP A string containing expression to be compiled \fIpattern\fP A string containing expression to be compiled
\fIlength\fP The length of the string or PCRE2_ZERO_TERMINATED \fIlength\fP The length of the string or PCRE2_ZERO_TERMINATED
\fIoptions\fP Option bits \fIoptions\fP Option bits
\fIerrorcode\fP Where to put an error code \fIerrorcode\fP Where to put an error code or NULL
\fIerroffset\fP Where to put an error offset \fIerroffset\fP Where to put an error offset
\fIccontext\fP Pointer to a compile context or NULL \fIccontext\fP Pointer to a compile context or NULL
.sp .sp

View File

@ -9812,17 +9812,17 @@ PCRE2_UCHAR *cworkspace = (PCRE2_UCHAR *)c16workspace;
/* -------------- Check arguments and set up the pattern ----------------- */ /* -------------- Check arguments and set up the pattern ----------------- */
/* There must be error code and offset pointers. */ /* There must be an error offset pointer. */
if (errorptr == NULL || erroroffset == NULL) return NULL; if (erroroffset == NULL) return NULL;
*errorptr = ERR0; if (errorptr) *errorptr = ERR0;
*erroroffset = 0; *erroroffset = 0;
/* There must be a pattern! */ /* There must be a pattern! */
if (pattern == NULL) if (pattern == NULL)
{ {
*errorptr = ERR16; if (errorptr) *errorptr = ERR16;
return NULL; return NULL;
} }
@ -9840,7 +9840,7 @@ if ((options & PCRE2_MATCH_INVALID_UTF) != 0) options |= PCRE2_UTF;
if ((options & ~PUBLIC_COMPILE_OPTIONS) != 0 || if ((options & ~PUBLIC_COMPILE_OPTIONS) != 0 ||
(ccontext->extra_options & ~PUBLIC_COMPILE_EXTRA_OPTIONS) != 0) (ccontext->extra_options & ~PUBLIC_COMPILE_EXTRA_OPTIONS) != 0)
{ {
*errorptr = ERR17; if (errorptr) *errorptr = ERR17;
return NULL; return NULL;
} }
@ -9848,7 +9848,7 @@ if ((options & PCRE2_LITERAL) != 0 &&
((options & ~PUBLIC_LITERAL_COMPILE_OPTIONS) != 0 || ((options & ~PUBLIC_LITERAL_COMPILE_OPTIONS) != 0 ||
(ccontext->extra_options & ~PUBLIC_LITERAL_COMPILE_EXTRA_OPTIONS) != 0)) (ccontext->extra_options & ~PUBLIC_LITERAL_COMPILE_EXTRA_OPTIONS) != 0))
{ {
*errorptr = ERR92; if (errorptr) *errorptr = ERR92;
return NULL; return NULL;
} }
@ -9860,7 +9860,7 @@ if ((zero_terminated = (patlen == PCRE2_ZERO_TERMINATED)))
if (patlen > ccontext->max_pattern_length) if (patlen > ccontext->max_pattern_length)
{ {
*errorptr = ERR88; if (errorptr) *errorptr = ERR88;
return NULL; return NULL;
} }
@ -10144,7 +10144,7 @@ if (parsed_size_needed >= PARSED_PATTERN_DEFAULT_SIZE)
(parsed_size_needed + 1) * sizeof(uint32_t), ccontext->memctl.memory_data); (parsed_size_needed + 1) * sizeof(uint32_t), ccontext->memctl.memory_data);
if (heap_parsed_pattern == NULL) if (heap_parsed_pattern == NULL)
{ {
*errorptr = ERR21; if (errorptr) *errorptr = ERR21;
goto EXIT; goto EXIT;
} }
cb.parsed_pattern = heap_parsed_pattern; cb.parsed_pattern = heap_parsed_pattern;
@ -10614,7 +10614,7 @@ HAD_EARLY_ERROR:
*erroroffset = ptr - pattern; *erroroffset = ptr - pattern;
HAD_ERROR: HAD_ERROR:
*errorptr = errorcode; if (errorptr) *errorptr = errorcode;
pcre2_code_free(re); pcre2_code_free(re);
re = NULL; re = NULL;
goto EXIT; goto EXIT;

View File

@ -1998,7 +1998,7 @@ static int run_invalid_utf8_test(const struct invalid_utf8_regression_test_case
int pattern_index, int i, pcre2_compile_context_8 *ccontext, pcre2_match_data_8 *mdata) int pattern_index, int i, pcre2_compile_context_8 *ccontext, pcre2_match_data_8 *mdata)
{ {
pcre2_code_8 *code; pcre2_code_8 *code;
int result, errorcode; int result;
PCRE2_SIZE length, erroroffset; PCRE2_SIZE length, erroroffset;
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer_8(mdata); PCRE2_SIZE *ovector = pcre2_get_ovector_pointer_8(mdata);
@ -2006,7 +2006,7 @@ static int run_invalid_utf8_test(const struct invalid_utf8_regression_test_case
return 1; return 1;
code = pcre2_compile_8((PCRE2_UCHAR8*)current->pattern[i], PCRE2_ZERO_TERMINATED, code = pcre2_compile_8((PCRE2_UCHAR8*)current->pattern[i], PCRE2_ZERO_TERMINATED,
current->compile_options, &errorcode, &erroroffset, ccontext); current->compile_options, NULL, &erroroffset, ccontext);
if (!code) { if (!code) {
printf("Pattern[%d:0] cannot be compiled. Error offset: %d\n", pattern_index, (int)erroroffset); printf("Pattern[%d:0] cannot be compiled. Error offset: %d\n", pattern_index, (int)erroroffset);
@ -2207,7 +2207,7 @@ static int run_invalid_utf16_test(const struct invalid_utf16_regression_test_cas
int pattern_index, int i, pcre2_compile_context_16 *ccontext, pcre2_match_data_16 *mdata) int pattern_index, int i, pcre2_compile_context_16 *ccontext, pcre2_match_data_16 *mdata)
{ {
pcre2_code_16 *code; pcre2_code_16 *code;
int result, errorcode; int result;
PCRE2_SIZE length, erroroffset; PCRE2_SIZE length, erroroffset;
const PCRE2_UCHAR16 *input; const PCRE2_UCHAR16 *input;
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer_16(mdata); PCRE2_SIZE *ovector = pcre2_get_ovector_pointer_16(mdata);
@ -2216,7 +2216,7 @@ static int run_invalid_utf16_test(const struct invalid_utf16_regression_test_cas
return 1; return 1;
code = pcre2_compile_16(current->pattern[i], PCRE2_ZERO_TERMINATED, code = pcre2_compile_16(current->pattern[i], PCRE2_ZERO_TERMINATED,
current->compile_options, &errorcode, &erroroffset, ccontext); current->compile_options, NULL, &erroroffset, ccontext);
if (!code) { if (!code) {
printf("Pattern[%d:0] cannot be compiled. Error offset: %d\n", pattern_index, (int)erroroffset); printf("Pattern[%d:0] cannot be compiled. Error offset: %d\n", pattern_index, (int)erroroffset);
@ -2394,7 +2394,7 @@ static int run_invalid_utf32_test(const struct invalid_utf32_regression_test_cas
int pattern_index, int i, pcre2_compile_context_32 *ccontext, pcre2_match_data_32 *mdata) int pattern_index, int i, pcre2_compile_context_32 *ccontext, pcre2_match_data_32 *mdata)
{ {
pcre2_code_32 *code; pcre2_code_32 *code;
int result, errorcode; int result;
PCRE2_SIZE length, erroroffset; PCRE2_SIZE length, erroroffset;
const PCRE2_UCHAR32 *input; const PCRE2_UCHAR32 *input;
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer_32(mdata); PCRE2_SIZE *ovector = pcre2_get_ovector_pointer_32(mdata);
@ -2403,7 +2403,7 @@ static int run_invalid_utf32_test(const struct invalid_utf32_regression_test_cas
return 1; return 1;
code = pcre2_compile_32(current->pattern[i], PCRE2_ZERO_TERMINATED, code = pcre2_compile_32(current->pattern[i], PCRE2_ZERO_TERMINATED,
current->compile_options, &errorcode, &erroroffset, ccontext); current->compile_options, NULL, &erroroffset, ccontext);
if (!code) { if (!code) {
printf("Pattern[%d:0] cannot be compiled. Error offset: %d\n", pattern_index, (int)erroroffset); printf("Pattern[%d:0] cannot be compiled. Error offset: %d\n", pattern_index, (int)erroroffset);