Skip to content

Commit fed1b0c

Browse files
authored
Add missing end-of-string checks to RegExp parser in unicode mode (#3875)
Fixes #3870. Fixes #3871. JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai [email protected]
1 parent b7a641c commit fed1b0c

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

jerry-core/parser/regexp/re-parser.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -612,30 +612,28 @@ re_parse_char_escape (re_compiler_ctx_t *re_ctx_p) /**< RegExp compiler context
612612
#if ENABLED (JERRY_ES2015)
613613
if (re_ctx_p->flags & RE_FLAG_UNICODE)
614614
{
615-
if (*re_ctx_p->input_curr_p == LIT_CHAR_LEFT_BRACE)
615+
if (re_ctx_p->input_curr_p + 1 < re_ctx_p->input_end_p
616+
&& re_ctx_p->input_curr_p[0] == LIT_CHAR_LEFT_BRACE
617+
&& lit_char_is_hex_digit (re_ctx_p->input_curr_p[1]))
616618
{
617-
re_ctx_p->input_curr_p++;
619+
lit_code_point_t cp = lit_char_hex_to_int (re_ctx_p->input_curr_p[1]);
620+
re_ctx_p->input_curr_p += 2;
618621

619-
if (re_ctx_p->input_curr_p < re_ctx_p->input_end_p && lit_char_is_hex_digit (*re_ctx_p->input_curr_p))
622+
while (re_ctx_p->input_curr_p < re_ctx_p->input_end_p && lit_char_is_hex_digit (*re_ctx_p->input_curr_p))
620623
{
621-
lit_code_point_t cp = lit_char_hex_to_int (*re_ctx_p->input_curr_p++);
624+
cp = cp * 16 + lit_char_hex_to_int (*re_ctx_p->input_curr_p++);
622625

623-
while (re_ctx_p->input_curr_p < re_ctx_p->input_end_p && lit_char_is_hex_digit (*re_ctx_p->input_curr_p))
626+
if (JERRY_UNLIKELY (cp > LIT_UNICODE_CODE_POINT_MAX))
624627
{
625-
cp = cp * 16 + lit_char_hex_to_int (*re_ctx_p->input_curr_p++);
626-
627-
if (JERRY_UNLIKELY (cp > LIT_UNICODE_CODE_POINT_MAX))
628-
{
629-
return ecma_raise_syntax_error (ECMA_ERR_MSG ("Invalid unicode escape sequence"));
630-
}
628+
return ecma_raise_syntax_error (ECMA_ERR_MSG ("Invalid unicode escape sequence"));
631629
}
630+
}
632631

633-
if (re_ctx_p->input_curr_p < re_ctx_p->input_end_p && *re_ctx_p->input_curr_p == LIT_CHAR_RIGHT_BRACE)
634-
{
635-
re_ctx_p->input_curr_p++;
636-
re_ctx_p->token.value = cp;
637-
break;
638-
}
632+
if (re_ctx_p->input_curr_p < re_ctx_p->input_end_p && *re_ctx_p->input_curr_p == LIT_CHAR_RIGHT_BRACE)
633+
{
634+
re_ctx_p->input_curr_p++;
635+
re_ctx_p->token.value = cp;
636+
break;
639637
}
640638
}
641639

@@ -867,7 +865,8 @@ re_parse_next_token (re_compiler_ctx_t *re_ctx_p) /**< RegExp compiler context *
867865

868866
#if ENABLED (JERRY_ES2015)
869867
if (re_ctx_p->flags & RE_FLAG_UNICODE
870-
&& lit_is_code_point_utf16_high_surrogate (ch))
868+
&& lit_is_code_point_utf16_high_surrogate (ch)
869+
&& re_ctx_p->input_curr_p < re_ctx_p->input_end_p)
871870
{
872871
const ecma_char_t next = lit_cesu8_peek_next (re_ctx_p->input_curr_p);
873872
if (lit_is_code_point_utf16_low_surrogate (next))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
assert (new RegExp("\ud800", "u").exec("\ud800")[0] === "\ud800");
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
try {
16+
new RegExp('"\\u', 'u');
17+
assert (false);
18+
} catch (e) {
19+
assert (e instanceof SyntaxError);
20+
}

0 commit comments

Comments
 (0)