Repository navigation
Fix BigInt error handling in pow and string comparisons - #1977
Open
NicolasBonet wants to merge 1 commit into
Open
NicolasBonet wants to merge 1 commit into
NicolasBonet wants to merge 1 commit into
Conversation
js_bigint_pow leaked its partial result when a multiplication failed. js_relational_slow and js_eq_slow treated an exception from JS_StringToBigInt as an invalid BigInt literal. The comparison returned false and left the exception pending, e.g. '1'.repeat(1e6) == 1n.
This branch has not been deployed
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.
This fixes two bugs in BigInt error paths.
1.
js_bigint_powleaks memory when a multiplication failsThe square-and-multiply loop keeps the partial result in
r. On each step it multiplies into a newr1, then freesr. Whenjs_bigint_mulfails (for example under a memory limit), the function returnedNULLwithout freeingr. The fix freesrbefore returning.2. Comparing a string with a BigInt hides exceptions
To compare a string with a BigInt (
==,!=,<,<=,>,>=),js_eq_slowandjs_relational_slowfirst convert the string withJS_StringToBigInt. That call returns one of three things:NaNwhen the string isn't a valid BigInt literal, and the comparison isfalseas the spec requiresJS_EXCEPTIONwhen the conversion fails, for example on out-of-memory or when the number is too largeBoth functions only checked whether the result was a BigInt, so
JS_EXCEPTIONtook the "invalid literal" path. The comparison returnedfalseand the exception was left pending on the context:The fix checks for
JS_EXCEPTIONafter each conversion and propagates it, so the comparison throws the sameRangeErrorasBigInt().Tests
New
api-test.ccases cover both bugs and fail without the fix:bigint_pow_oomreports the leak throughJS_ABORT_ON_LEAKS, andbigint_string_compare_exceptionchecks that each comparison throws.tests.confand the test262 BigInt, comparison and exponentiation directories pass.