Resolve calc() division by zero to infinity instead of crashing - #2819
Merged
Merged
Conversation
A division by zero inside a calc() expression (e.g. width: calc(100px / 0), or a divisor that evaluates to zero) raised ZeroDivisionError and aborted the whole render. CSS Values and Units 4 requires such a division to resolve to +/-infinity or NaN following IEEE-754, which WeasyPrint already supports for calc(infinity * ...) expressions; produce those values instead of dividing.
Member
|
Thanks for the fix! |
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 bug
A division by zero inside a
calc()expression raisesZeroDivisionErrorand aborts the entire render:It also fires when the divisor merely evaluates to zero, e.g.
calc(100px / (1 - 1)).Why the expected behaviour is unambiguous
CSS Values and Units 4 §10.10 requires a division by zero to resolve to
+∞,−∞, orNaNfollowing IEEE-754, not to be an error: a positive numerator gives+∞, a negative one−∞, and0 / 0givesNaN. WeasyPrint already supports infinite and NaN values incalc()(calc(infinity * 1px),calc(nan * 1px), and theclamp()tests usecalc(-infinity * 1px)), so only the division operator was missing the case.The fix
In
_resolve_calc_product, when the divisor is zero, produce±infinity/NaN(IEEE-754) instead of dividing. The result is identical to the equivalentcalc(infinity * …)/calc(nan * …)expression, which then flows through the existing infinity handling (an infinite width renders asinf, a negative-infinite/NaN width clamps to0, exactly as the keyword forms already do).Tests
test_calc_division_by_zeroasserts the rendered width of each division-by-zero form equals that of itscalc(infinity * …)/calc(nan * …)counterpart — covering positive/negative numerators, relative units, a divisor that evaluates to zero,0 / 0(NaN), and infinity propagating through a following+/*. All eight cases fail onmainwith the originalZeroDivisionErrorand pass with the fix; the fulltests/css/test_math.py(439 passed) andtest_validation.pystay green,ruff checkclean.