Skip to content

match_next: validate pointer arguments and pattern reference in pcre2_next_match - #1053

Open
akashdeshmukh-git wants to merge 1 commit into
PCRE2Project:mainfrom
akashdeshmukh-git:validate-next-match-args
Open

akashdeshmukh-git wants to merge 1 commit into
PCRE2Project:mainfrom
akashdeshmukh-git:validate-next-match-args

Conversation

@akashdeshmukh-git

Copy link
Copy Markdown

pcre2_next_match() unconditionally dereferenced its pointer arguments (match_data->start_offset, match_data->ovector) without validating that match_data, pstart_offset, or poptions were non-NULL. Additionally, the internal do_bumpalong() helper dereferenced match_data->code to query newline and UTF options without verifying that match_data->code is non-NULL.

This change:

  1. Adds PCRE2_ASSERT() preconditions and returns FALSE early if match_data, pstart_offset, or poptions is NULL.
  2. Guards match_data->code and match_data->subject in do_bumpalong() when inspecting newline conventions and advancing across code units.
  3. Extends unittest() in src/pcre2test_inc.h with test coverage verifying that NULL arguments safely return FALSE.
  4. Documents the NULL pointer return behaviour in doc/pcre2api.adoc and doc/pcre2_next_match.adoc.

Comment thread src/pcre2_match_next.c Outdated
Comment on lines +105 to +106
if (match_data == NULL || pstart_offset == NULL || poptions == NULL)
return FALSE;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should not add runtime checks for preconditions.

Comment thread src/pcre2_match_next.c Outdated
PCRE2_SIZE subject_length = match_data->subject_length;
#ifdef SUPPORT_UNICODE
BOOL utf = (match_data->code->overall_options & PCRE2_UTF) != 0;
BOOL utf = (match_data->code != NULL && (match_data->code->overall_options & PCRE2_UTF) != 0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Interesting - elsewhere in PCRE2, do we make the assumption that ->code fields are non-null if match_data->rc is a success?

It's possible that we do. What was the scenario that you wanted to guard against here, in terms of a practical use-case?

I can only imagine here that the match_data has simply not even been passed to pcre2_match.

@akashdeshmukh-git

Copy link
Copy Markdown
Author

@NWilson Thanks. You're right: after a successful match code is always set; it can only be unset if the match_data was never passed to a match function, which is a caller error. I've reduced the PR to precondition assertions only, as on #1052: no runtime checks, and the do_bumpalong guards, doc and test changes are dropped.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants