Skip to content

Commit 8279487

Browse files
committed
[MERGE #5311 @dilijev] Fix #5182: Clamp range quantifier to MaxCharCount instead of throwing SyntaxError
Merge pull request #5311 from dilijev:regex-quantifier-noerror Fix #5182
2 parents c198502 + 0a51428 commit 8279487

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

lib/Parser/RegexParser.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,18 +1120,42 @@ namespace UnifiedRegex
11201120
if (!standardEncodedChars->IsDigit(ec))
11211121
{
11221122
if (digits == 0)
1123+
{
11231124
Fail(JSERR_RegExpSyntax);
1125+
}
1126+
11241127
return n;
11251128
}
1129+
11261130
if (n > MaxCharCount / 10)
1127-
Fail(JSERR_RegExpSyntax);
1131+
{
1132+
break;
1133+
}
1134+
11281135
n *= 10;
11291136
if (n > MaxCharCount - standardEncodedChars->DigitValue(ec))
1130-
Fail(JSERR_RegExpSyntax);
1137+
{
1138+
break;
1139+
}
1140+
11311141
n += standardEncodedChars->DigitValue(ec);
11321142
digits++;
11331143
ECConsume();
11341144
}
1145+
1146+
Assert(digits != 0); // shouldn't be able to reach here with (digits == 0)
1147+
1148+
// The token had a value larger than MaxCharCount so we didn't return the value and reached here instead.
1149+
// Consume the rest of the token and return MaxCharCount.
1150+
while (true)
1151+
{
1152+
EncodedChar ec = ECLookahead();
1153+
if (!standardEncodedChars->IsDigit(ec))
1154+
{
1155+
return MaxCharCount;
1156+
}
1157+
ECConsume();
1158+
}
11351159
}
11361160

11371161
template <typename P, const bool IsLiteral>

test/Regex/clampNumericQuantifier.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//-------------------------------------------------------------------------------------------------------
2+
// Copyright (C) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4+
//-------------------------------------------------------------------------------------------------------
5+
6+
function assert(b) {
7+
if (!b) {
8+
print('fail');
9+
}
10+
}
11+
12+
let p1 = new RegExp('^[a-z]{2,2147483648}$');
13+
assert(!p1.test('a'));
14+
15+
let p2 = /^[a-z]{2,2147483648}$/;
16+
assert(p2.test('aaaaa'));
17+
18+
print('PASS');

test/Regex/rlexe.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
<compile-flags>-ES6RegExPrototypeProperties-</compile-flags>
3838
</default>
3939
</test>
40+
<test>
41+
<default>
42+
<files>clampNumericQuantifier.js</files>
43+
</default>
44+
</test>
4045
<test>
4146
<default>
4247
<files>rx1.js</files>

0 commit comments

Comments
 (0)