Skip to content

Guard the string table reuse bounds for normalized literals - #65538

Closed
Josué Barros (joshbarros) wants to merge 1 commit into
microsoft:mainfrom
joshbarros:fix/65451-numeric-literal-stringtable-bounds
Closed

Josué Barros (joshbarros) wants to merge 1 commit into
microsoft:mainfrom
joshbarros:fix/65451-numeric-literal-stringtable-bounds

Conversation

@joshbarros

Copy link
Copy Markdown

Fixes #65451

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 start := end - len(text) before slicing fileText. In these cases the start is negative, so the existing fallback that stores the text in otherStrings was never reached and the encoder panicked with slice bounds out of range.

This checks start >= 0 before the slice. The add is scoped to the reuse fast path.

The new encoder test covers .1, 1e3, 1e9, 1e2, 1 and 0; 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.

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.
Copilot AI balanced review requested due to automatic review settings November 16, 2026 19:04
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 16, 2026
@typescript-automation typescript-automation Bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Nov 16, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@github-project-automation github-project-automation Bot moved this from Not started to Done in PR Backlog Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Native API createSourceFile panics for numeric literals whose normalized text is longer than the source

3 participants