-
Notifications
You must be signed in to change notification settings - Fork 205
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
match: avoid crash if subject NULL and PCRE2_ZERO_TERMINATED #53
Conversation
I also noticed there's currently an error when |
that is by design, subject can never be NULL and if it does pcre2_match() returns and error with equivalent message "NULL argument passed". it is a little subtle, but in C, NULL is the address of a variable outside your memory space (hence invalid), while "" is the address of a variable that contains the NUL('\0') character in your memory space (therefore valid).
when the subject is a valid address, then length of 0 implies it is an "empty string" and even matches a pattern like "^$", NULL cannot be accessed and therefore can't match anything, hence why the API make it clear by returning an error. hopefully the following hacky example code could help clarify:
|
I usually prefer to support |
As the first step towards allowing a NULL string to represent an empty one, and as an alternative fix for the failures addressed by PCRE2Project#53
@carenas I think there's a misunderstanding. What I meant is that when From a quick glance at your change, it looks like you handle a NULL subject with a If you prefer, go with your original change, and I'll submit a separate PR later on. I didn't want to hijack your PR, sorry for that. |
When length of subject is PCRE2_ZERO_TERMINATED strlen is used to calculate its size, which will trigger a crash if subject is also NULL. Move the NULL check before strlen on it would be used, and make sure or dependent variables are set after the NULL validation as well. While at it, fix a typo in a debug flag in the same file, which is otherwise unrelated and make sure the full section of constrain checks can be identified clearly using the leading comment alone.
When length of subject is PCRE2_ZERO_TERMINATED strlen is used to calculate its size, which will trigger a crash if subject is also NULL. Move the NULL check before the detection for subject sizes to avoid this issue.
The underlying pcre2_match() function will validate the subject if needed, but will crash when length is PCRE2_ZERO_TERMINATED or if subject == NULL and pcre2_match() is not being called because match_data was provided. The replacement parameter is missing NULL checks, and so currently allows for an equivalent response to "" if rlength == 0. Restrict all other cases to avoid strlen(NULL) crashes in the same way that is done for subject, but also make sure to reject invalid length values as early as possible.
Probably not that critical, as it has been there since the very beginning but seems like a better behaviour to have when using PCRE2_ZERO_TERMINATED and matches better the documentation.
Problem also exist in pcre2_dfa_match and POSIX (but there crashing is the expected behaviour), as well as in pcre2_substitute where it could crash even when not using PCRE2_ZERO_TERMINATED, so split onto multiple patches for easy of reviewing.
pcre2_substitute needs a lot more checks and will have to keep an exception as the replacement string was not checked before and it just happens to work fine regardless when the length was given as 0.
Tests are still missing and I am not sure of the design required to implement them yet, but if that is deemed not critical (after all that is the current state, as there is no way to test NULL subjects, patterns or replacement strings as of now) then could be implemented as a follow up.