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

fix integer overflow when parsing Perl-extended named backrefs #246

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

cmazakas
Copy link
Member

This issue comes to us as: https://issues.oss-fuzz.com/issues/42512790

Note that Perl seems to reject the regex better than us, flagging \k-- as invalid in the first place.

We seem to treat \g and \k as literally the same, while Perl doesn't. Perl rejects \g-- as unterminated token.

Our code seems to parse \k- appropriately, with the assumption that it's a valid synonym for \g, but then we run into overflow issues when parsing the number generated by the fuzzer which is LONG_MIN.

I'm not going to change the behavior of \k here but instead I simply just tried my hand at squashing the overflows.

auto int_min = (std::numeric_limits<std::intmax_t>::min)();
auto int_max = (std::numeric_limits<std::intmax_t>::max)();

if ((i < 0) && (mark_count < int_min - i)) { i = -1; }
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this is correct: i < 0 implies an error occurred when parsing the number (negative indexes are always invalid right?), plus mark_count comes from an unsigned value, so is always > 0. Also if you debug the first test case in the new tests, then i ends up being set to 2, and only fails later "by chance" because there are no subsequent marked sub-expressions.

So I would be inclined to remove this line, and instead put the subsequent overflow checks inside a if(i >=0) block. Actually, I'm not sure what should happen if i = 0 either, but it's not handled at all at present, so I'm assuming that's definitely wrong ;)

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