Report overflowing durations as invalid instead of crashing - #1550
Closed
aryansk wants to merge 1 commit into
Closed
Report overflowing durations as invalid instead of crashing#1550aryansk wants to merge 1 commit into
aryansk wants to merge 1 commit into
Conversation
isoduration's parse_duration builds amounts with Decimal, and amounts past the context's Emax (an exponent like 1E1000000 or a digit run longer than 999999) raise decimal.Overflow - an ArithmeticError, not a DurationParsingException. That escaped the format checker uncaught. Include ArithmeticError in the checker's raises tuple so such strings are reported as invalid durations like every other malformed instance. Fixes python-jsonschema#1511 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
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.
What
Fixes #1511 — the
durationformat checker no longer crashes with an uncaughtdecimal.Overflowon strings whose amounts exceedDecimal's context limits; they are reported as invalid durations like any other malformed instance.Why
isoduration.parse_durationbuilds each component amount withDecimal(...). For an exponent past the context'sEmax— e.g.P1E1000000D— or a plain digit run longer than 999999,Decimalraisesdecimal.Overflow, which is not a subclass of the checker's declaredisoduration.DurationParsingException. The exception escapesFormatChecker.checkuncaught and propagates out ofiter_errors/validate:The boundary is exact:
P1E999999Dis accepted,P1E1000000Doverflows. The same bug is reachable without an exponent via a long digit run ("P" + "9" * 1000000 + "D").How
decimal.Overflowis anArithmeticError, so the checker'sraisestuple is broadened to(isoduration.DurationParsingException, ArithmeticError)— the same idiom sibling checkers already use (e.g.raises=(idna.IDNAError, UnicodeError), and the pattern proposed in #1526).FormatChecker.checkthen wraps it into aFormatErrorand the instance is reported as invalid.Tests
Added
TestFormatChecker.test_it_rejects_durations_that_overflow_decimal(skipped when the optionalisodurationdependency is not installed):P1E1000000Dand the 1M-digit-run variant both raiseFormatError(both crash withdecimal.Overflowbefore the fix).P1Y2M3DT4H5M6Sis still accepted.Full test suite passes (497 tests). Added a CHANGELOG entry.
🤖 Generated with Codebuff