Skip to content

Commit 9522325

Browse files
Copilotjakebailey
andauthored
Fix infinite hang in regex parser for invalid UTF-8 with unicode flag (#3857)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent 6f58807 commit 9522325

5 files changed

Lines changed: 29 additions & 1 deletion

File tree

internal/scanner/regexp.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,12 @@ func (p *regExpParser) scanSourceCharacter() string {
10151015
return string(ch)
10161016
}
10171017
ch, size := utf8.DecodeRuneInString(p.text()[p.pos():])
1018-
if size == 0 || ch == utf8.RuneError {
1018+
if size == 0 {
1019+
return ""
1020+
}
1021+
if ch == utf8.RuneError {
1022+
// Invalid UTF-8; consume the byte to avoid infinite loops.
1023+
p.incPos(size)
10191024
return ""
10201025
}
10211026
p.incPos(size)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [tests/cases/compiler/regexInvalidUtf8WithUnicodeFlag.ts] ////
2+
3+
//// [regexInvalidUtf8WithUnicodeFlag.ts]
4+
//u
5+
6+
7+
//// [regexInvalidUtf8WithUnicodeFlag.js]
8+
"use strict";
9+
//u;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [tests/cases/compiler/regexInvalidUtf8WithUnicodeFlag.ts] ////
2+
3+
=== regexInvalidUtf8WithUnicodeFlag.ts ===
4+
5+
/�/u
6+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [tests/cases/compiler/regexInvalidUtf8WithUnicodeFlag.ts] ////
2+
3+
=== regexInvalidUtf8WithUnicodeFlag.ts ===
4+
/�/u
5+
>/�/u : RegExp
6+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// @target: esnext
2+
//u

0 commit comments

Comments
 (0)