Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pcre2_compile: make errorcode parameter optional #102

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/pcre2_compile.3
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ arguments are:
\fIpattern\fP A string containing expression to be compiled
\fIlength\fP The length of the string or PCRE2_ZERO_TERMINATED
\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
\fIccontext\fP Pointer to a compile context or NULL
.sp
Expand Down
18 changes: 9 additions & 9 deletions src/pcre2_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -9812,17 +9812,17 @@ PCRE2_UCHAR *cworkspace = (PCRE2_UCHAR *)c16workspace;

/* -------------- 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;
*errorptr = ERR0;
if (erroroffset == NULL) return NULL;
if (errorptr) *errorptr = ERR0;
Copy link
Contributor Author

@carenas carenas Apr 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noticed this (setting the error to ERR0 unless there was an error) is not documented, could it be dropped? (meaning errorptr content is indeterminate unless return is NULL).

forcefully setting the error offset to 0 is a little unusual, and might be ok to leave as an undocumented "feature" for historical reasons, but reminds me of another undocumented feature I recently found while migrating a codebase from PCRE to PCRE2, where the code was relying in the error offset from pcreexec() being returned with sub[0] and that was no longer valid in the equivalent pcre2 call.

FWIW I think the addition of pcre2_get_startchar() make the overall API cleaner so not complaining about that one, but surelly this one does allow for some overly clever ways to initialize variables as shown by my test code in #51 (comment)

*erroroffset = 0;

/* There must be a pattern! */

if (pattern == NULL)
{
*errorptr = ERR16;
if (errorptr) *errorptr = ERR16;
return NULL;
}

Expand All @@ -9840,15 +9840,15 @@ if ((options & PCRE2_MATCH_INVALID_UTF) != 0) options |= PCRE2_UTF;
if ((options & ~PUBLIC_COMPILE_OPTIONS) != 0 ||
(ccontext->extra_options & ~PUBLIC_COMPILE_EXTRA_OPTIONS) != 0)
{
*errorptr = ERR17;
if (errorptr) *errorptr = ERR17;
return NULL;
}

if ((options & PCRE2_LITERAL) != 0 &&
((options & ~PUBLIC_LITERAL_COMPILE_OPTIONS) != 0 ||
(ccontext->extra_options & ~PUBLIC_LITERAL_COMPILE_EXTRA_OPTIONS) != 0))
{
*errorptr = ERR92;
if (errorptr) *errorptr = ERR92;
return NULL;
}

Expand All @@ -9860,7 +9860,7 @@ if ((zero_terminated = (patlen == PCRE2_ZERO_TERMINATED)))

if (patlen > ccontext->max_pattern_length)
{
*errorptr = ERR88;
if (errorptr) *errorptr = ERR88;
return NULL;
}

Expand Down Expand Up @@ -10144,7 +10144,7 @@ if (parsed_size_needed >= PARSED_PATTERN_DEFAULT_SIZE)
(parsed_size_needed + 1) * sizeof(uint32_t), ccontext->memctl.memory_data);
if (heap_parsed_pattern == NULL)
{
*errorptr = ERR21;
if (errorptr) *errorptr = ERR21;
goto EXIT;
}
cb.parsed_pattern = heap_parsed_pattern;
Expand Down Expand Up @@ -10614,7 +10614,7 @@ ptr = pattern + cb.erroroffset;
*erroroffset = ptr - pattern;

HAD_ERROR:
*errorptr = errorcode;
if (errorptr) *errorptr = errorcode;
pcre2_code_free(re);
re = NULL;
goto EXIT;
Expand Down
12 changes: 6 additions & 6 deletions src/pcre2_jit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1998,15 +1998,15 @@ 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)
{
pcre2_code_8 *code;
int result, errorcode;
int result;
PCRE2_SIZE length, erroroffset;
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer_8(mdata);

if (current->pattern[i] == NULL)
return 1;

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) {
printf("Pattern[%d:0] cannot be compiled. Error offset: %d\n", pattern_index, (int)erroroffset);
Expand Down Expand Up @@ -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)
{
pcre2_code_16 *code;
int result, errorcode;
int result;
PCRE2_SIZE length, erroroffset;
const PCRE2_UCHAR16 *input;
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer_16(mdata);
Expand All @@ -2216,7 +2216,7 @@ static int run_invalid_utf16_test(const struct invalid_utf16_regression_test_cas
return 1;

code = pcre2_compile_16(current->pattern[i], PCRE2_ZERO_TERMINATED,
current->compile_options, &errorcode, &erroroffset, ccontext);
current->compile_options, NULL, &erroroffset, ccontext);

if (!code) {
printf("Pattern[%d:0] cannot be compiled. Error offset: %d\n", pattern_index, (int)erroroffset);
Expand Down Expand Up @@ -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)
{
pcre2_code_32 *code;
int result, errorcode;
int result;
PCRE2_SIZE length, erroroffset;
const PCRE2_UCHAR32 *input;
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer_32(mdata);
Expand All @@ -2403,7 +2403,7 @@ static int run_invalid_utf32_test(const struct invalid_utf32_regression_test_cas
return 1;

code = pcre2_compile_32(current->pattern[i], PCRE2_ZERO_TERMINATED,
current->compile_options, &errorcode, &erroroffset, ccontext);
current->compile_options, NULL, &erroroffset, ccontext);

if (!code) {
printf("Pattern[%d:0] cannot be compiled. Error offset: %d\n", pattern_index, (int)erroroffset);
Expand Down