fix: reject sign/radix prefix with no digits in BigInt parsing - #110
Open
spokodev wants to merge 1 commit into
Open
fix: reject sign/radix prefix with no digits in BigInt parsing#110spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Author
|
@googlebot I signed it! |
JSBI.BigInt() returned 0n for strings that are a sign or a radix prefix
followed by whitespace and no digits, where native BigInt() throws a
SyntaxError:
JSBI.BigInt("+ ") // 0n (native: SyntaxError)
JSBI.BigInt("- ") // 0n (native: SyntaxError)
JSBI.BigInt("0x ") // 0n (native: SyntaxError)
JSBI.BigInt("0b ") // 0n (native: SyntaxError)
JSBI.BigInt("0o ") // 0n (native: SyntaxError)
__fromString consumed the sign and/or 0x/0o/0b prefix but never checked
that at least one value digit followed. With an empty body the digit loop
breaks immediately on the whitespace and the trailing-whitespace cleanup
returns 0n instead of null. ECMAScript StringToBigInt requires at least
one digit after a sign or radix prefix.
Capture the cursor position where the value digits start and return null
when no digit was consumed after a sign or non-decimal radix prefix.
spokodev
force-pushed
the
fix/bigint-parse-sign-prefix-no-digits
branch
from
June 25, 2026 20:20
e8ffecd to
e096470
Compare
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.
Bug
JSBI.BigInt(string)silently returns0nfor strings that consist of a sign or a radix prefix followed by whitespace and no digits, where the nativeBigInt()parser throws aSyntaxError:Adjacent inputs are already handled correctly, which is why this slipped through:
"+"and"0x"(no trailing whitespace) throw,""/" "parse to0n, and"+0"/" 0 "parse fine. The break is specifically a sign/prefix plus trailing whitespace with no digit in between.Spec
ECMAScript
StringToBigInt(viaStringIntegerLiteral) requires at least one digit after a sign or a0x/0o/0bradix prefix. A sign or prefix with no following digit is not a valid integer literal, so it must produceSyntaxError, not0n.Root cause
__fromStringinlib/jsbi.tsconsumes an optional sign and an optional0x/0o/0bprefix, then enters the digit-parsing loop. When the remaining body is empty (the next char is whitespace), the digit loop breaks immediately and the result stays0. The trailing-whitespace cleanup at the end of the function then accepts the whitespace and returns the zero result instead ofnull. Nothing verifies that at least one value digit was actually consumed after a sign or prefix.Fix
Record the cursor position where the value digits begin (after the sign, the radix prefix, and any leading zeros). After the digit loop, return
nullwhen the cursor has not moved and no leading zero was seen, but a sign or non-decimal radix was present:leadingZeroguards the legitimate"0"/"0x0"style inputs that set it before the loop. Thesign !== 0 || radix !== 10condition keeps the existing behavior for a bare empty / whitespace string (which has no sign and decimal radix and must remain0n).Verification
tests/tests.mjs. Before the fix the assertion fails (the values parse to0ninstead of throwing); after the fix it passes.npm test).JSBI.BigIntagainst nativeBigInt: 0 divergences, no regression on any previously valid input.