Repository navigation
Guard the string table reuse bounds for normalized literals - #65538
Closed
Josué Barros (joshbarros) wants to merge 1 commit into
Closed
Josué Barros (joshbarros) wants to merge 1 commit into
Josué Barros (joshbarros) wants to merge 1 commit into
Conversation
A numeric literal such as .1 or 1e3 normalizes to a longer text than its source spelling. recordExtendedData_NumericLiteral passes that normalized text with the original end position, and stringTable.add computed a negative start before slicing fileText. Check the start bound so the existing fallback to otherStrings is used. Adds encoder coverage for .1, 1e3, 1e9, 1e2, 1 and 0.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues with the numeric-literal encoding fix.
No open issues 🎉
What changed in this PR
Fixes #65451 by preventing the native API’s source-file encoder from panicking when a numeric literal’s normalized text is longer than its source spelling.
Changes:
- Guards against negative slice starts in the string-table reuse path, allowing normalized text to use the existing fallback.
- Adds regression coverage for three panic-triggering literals and three non-crashing cases.
| File | Description |
|---|---|
tsc/internal/api/encoder/stringtable.go |
Checks the computed start before slicing source text. |
tsc/internal/api/encoder/encoder_test.go |
Tests encoding of normalized numeric literals and control cases. |
Review effort: Balanced
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.
Fixes #65451
A numeric literal such as
.1or1e3normalizes to a longer text than its source spelling.recordExtendedData_NumericLiteralpasses that normalized text with the original end position, andstringTable.addcomputedstart := end - len(text)before slicingfileText. In these cases the start is negative, so the existing fallback that stores the text inotherStringswas never reached and the encoder panicked withslice bounds out of range.This checks
start >= 0before the slice. The add is scoped to the reuse fast path.The new encoder test covers
.1,1e3,1e9,1e2,1and0; it panics without the change.Validation:
go test ./tsc/internal/api/encoder -run TestEncodeSourceFileWithNormalizedNumericLiterals -count=1: panics without the change, passes with it.go test ./tsc/internal/api/encoder ./tsc/internal/api -count=1: pass.gofmt: clean.Drafted with assistance from an AI assistant. I reviewed the diff and ran the tests above.