Skip to content

fix(65076): treat literal U+FFFD as a real rune in regex classes - #65080

Open
Timur Vural (timurzvural) wants to merge 1 commit into
microsoft:mainfrom
timurzvural:fix/65076-regexp-fffd
Open

Timur Vural (timurzvural) wants to merge 1 commit into
microsoft:mainfrom
timurzvural:fix/65076-regexp-fffd

Conversation

@timurzvural

Copy link
Copy Markdown

Fixes #65076.

Bug

utf8.RuneError is defined as U+FFFD. utf8.DecodeRune returns that value for both a real U+FFFD (size 3) and an invalid UTF-8 byte (size 1). scanSourceCharacter treated any ch == utf8.RuneError as a decode failure and consumed a single raw byte (0xEF, the lead byte of UTF-8 U+FFFD).

A class like /[\uFFF9-�]/ was therefore checked as U+FFF9 … 0xEF, producing false TS1517 (and TS1516 with the u flag). Escaped \uFFFD was fine; TypeScript 6.0.3 was fine.

Fix

Only treat ch == utf8.RuneError && size == 1 as invalid UTF-8 (same pattern already used in the printer). A real U+FFFD advances by 3 bytes and participates in the range normally.

Tests

TestRegExpLiteralReplacementCharacterInClassRange covers the #65076 repro, the u-flag variant, escaped \uFFFD, a lone literal U+FFFD, and a range that starts with literal U+FFFD.

AI assistance disclosure

This PR was authored with assistance from Cursor Agent. I have read and understand the contents of this PR.

utf8.RuneError is U+FFFD; only size==1 means invalid UTF-8. The old
check treated a real replacement character as a decode error and fed
0xEF into character-class range checks.

Co-authored-by: Cursor <cursoragent@cursor.com>
Copilot AI balanced review requested due to automatic review settings October 31, 2026 22:10
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Oct 31, 2026
@typescript-automation typescript-automation Bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Oct 31, 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

No active review issues are reported for the regex scanner’s U+FFFD handling fix.

No open issues 🎉

What changed in this PR

Fixes #65076 by distinguishing literal U+FFFD characters from invalid UTF-8 in the native compiler’s regex scanner, preventing false character-class range diagnostics.

Changes:

  • Treats utf8.RuneError as invalid UTF-8 only when decoding consumes one byte.
  • Adds scanner regression tests for literal and escaped U+FFFD, including range boundaries and the u flag.
File Description
tsc/​internal/​scanner/​scanner_test.go Adds five regression cases covering U+FFFD handling in regex literals.
tsc/​internal/​scanner/​regexp.go Corrects UTF-8 decoding checks and handles empty input separately.

Review effort: Balanced

This branch has not been deployed

No deployments
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

2 participants