Report oversized regex repeat count as invalid instead of crashing#1526
Open
vineethsaivs wants to merge 1 commit into
Open
Report oversized regex repeat count as invalid instead of crashing#1526vineethsaivs wants to merge 1 commit into
vineethsaivs wants to merge 1 commit into
Conversation
The "regex" format checker declared raises=re.error, but re.compile
raises OverflowError (not an re.error subclass) when a repetition count
is too large, e.g. "a{99999999999999}". That OverflowError escaped
FormatChecker.check uncaught, crashing conforms()/iter_errors()/validate
instead of reporting the string as an invalid regex, unlike every other
malformed pattern which is caught via re.error.
Declare raises=(re.error, OverflowError) so the oversized-repeat case is
reported as an invalid regex like the rest. Add a regression test.
Documentation build overview
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
regexformat checker is declared withraises=re.error:FormatChecker.checkonly converts the declared exception(s) into aFormatError. Butre.compileraisesOverflowError(which is not anre.errorsubclass) when a repetition count is too large, e.g.re.compile("a{99999999999999}"). ThatOverflowErrorescapes uncaught, so aregex-format check on such a string crashes instead of reporting it as an invalid regex, unlike every other malformed pattern (which raisesre.errorand is caught).The same escape happens through
iter_errors/validatewhenformatchecking is enabled.Fix
Declare the checker with
raises=(re.error, OverflowError)so the oversized-repeat case is reported as an invalid regex like the rest. This matches sibling checkers that already pass exception tuples (e.g.raises=(idna.IDNAError, UnicodeError)). No behavior change for valid patterns or forre.errorcases.Tests
Added
TestFormatChecker.test_regex_format_rejects_overflowing_repeat_count, mirroring the existingtest_format_checkers_come_with_defaults(useschecker.check(...)+assertRaises(FormatError)).OverflowError.conforms("a{99999999999999}", "regex")returnsFalse, whileconforms("[unterminated", "regex")staysFalseandconforms("valid.*regex", "regex")staysTrue.python -m pytest jsonschema/tests/test_format.py-> 9 passed.Added a CHANGELOG entry.