Skip to content

Commit 793c0e5

Browse files
committed
More rigorously check surrogate pairs in regexp validator
1 parent b5c1787 commit 793c0e5

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

acorn/src/regexp.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export class RegExpValidationState {
5050
if (!this.switchU || c <= 0xD7FF || c >= 0xE000 || i + 1 >= l) {
5151
return c
5252
}
53-
return (c << 10) + s.charCodeAt(i + 1) - 0x35FDC00
53+
const next = s.charCodeAt(i + 1)
54+
return next >= 0xDC00 && next <= 0xDFFF ? (c << 10) + next - 0x35FDC00 : c
5455
}
5556

5657
nextIndex(i) {
@@ -59,8 +60,9 @@ export class RegExpValidationState {
5960
if (i >= l) {
6061
return l
6162
}
62-
const c = s.charCodeAt(i)
63-
if (!this.switchU || c <= 0xD7FF || c >= 0xE000 || i + 1 >= l) {
63+
let c = s.charCodeAt(i), next
64+
if (!this.switchU || c <= 0xD7FF || c >= 0xE000 || i + 1 >= l ||
65+
(next = s.charCodeAt(i + 1)) < 0xDC00 || next > 0xDFFF) {
6466
return i + 1
6567
}
6668
return i + 2

test/tests-regexp.js

+1
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,7 @@ test("/[\\d][\\12-\\14]{1,}[^\\d]/", {}, { ecmaVersion: 2015 })
10491049
testFail("/[\\d][\\12-\\14]{1,}[^\\d]/u", "Invalid regular expression flag (1:1)", { ecmaVersion: 5 })
10501050
testFail("/[\\d][\\12-\\14]{1,}[^\\d]/u", "Invalid regular expression: /[\\d][\\12-\\14]{1,}[^\\d]/: Invalid class escape (1:1)", { ecmaVersion: 2015 })
10511051
test("/([a ]\\b)*\\b/", {}, { ecmaVersion: 5 })
1052+
test("/[x-*]/u".replace("*", String.fromCharCode(0xd800)), {}, {ecmaVersion: 6})
10521053

10531054
/*
10541055
// This is test case generator.

0 commit comments

Comments
 (0)