Skip to content

Commit 025596a

Browse files
committed
Raise parser error on invalid unicode escape sequence
JerryScript-DCO-1.0-Signed-off-by: Máté Tokodi [email protected]
1 parent 2dbb6f7 commit 025596a

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

Diff for: jerry-core/parser/js/js-lexer.c

+34-8
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ lexer_hex_in_braces_to_code_point (const uint8_t *source_p, /**< current source
147147
/**
148148
* Parse hexadecimal character sequence
149149
*
150-
* @return character value
150+
* @return character value (-1 if the escape sequence is invalid)
151151
*/
152152
static lit_code_point_t
153153
lexer_unchecked_hex_to_character (const uint8_t **source_p) /**< [in, out] current source position */
@@ -174,13 +174,19 @@ lexer_unchecked_hex_to_character (const uint8_t **source_p) /**< [in, out] curre
174174
}
175175
else
176176
{
177-
JERRY_ASSERT ((byte >= LIT_CHAR_LOWERCASE_A && byte <= LIT_CHAR_LOWERCASE_F)
178-
|| (byte >= LIT_CHAR_UPPERCASE_A && byte <= LIT_CHAR_UPPERCASE_F));
177+
if (!((byte >= LIT_CHAR_LOWERCASE_A && byte <= LIT_CHAR_LOWERCASE_F)
178+
|| (byte >= LIT_CHAR_UPPERCASE_A && byte <= LIT_CHAR_UPPERCASE_F)))
179+
{
180+
return (lit_code_point_t) -1;
181+
}
179182

180183
result += LEXER_TO_ASCII_LOWERCASE (byte) - (LIT_CHAR_LOWERCASE_A - 10);
181184
}
182185

183-
JERRY_ASSERT (result <= LIT_UNICODE_CODE_POINT_MAX);
186+
if (result > LIT_UNICODE_CODE_POINT_MAX)
187+
{
188+
return (lit_code_point_t) -1;
189+
}
184190

185191
if (length == 0)
186192
{
@@ -2068,8 +2074,10 @@ lexer_scan_private_identifier (parser_context_t *context_p) /**< context */
20682074

20692075
/**
20702076
* Convert an ident with escapes to a utf8 string.
2077+
*
2078+
* @return false if source contains invalid unicode escape sequence, true otherwise
20712079
*/
2072-
void
2080+
bool
20732081
lexer_convert_ident_to_cesu8 (uint8_t *destination_p, /**< destination string */
20742082
const uint8_t *source_p, /**< source string */
20752083
prop_length_t length) /**< length of destination string */
@@ -2083,7 +2091,12 @@ lexer_convert_ident_to_cesu8 (uint8_t *destination_p, /**< destination string */
20832091
if (*source_p == LIT_CHAR_BACKSLASH)
20842092
{
20852093
source_p += 2;
2086-
destination_p += lit_code_point_to_cesu8_bytes (destination_p, lexer_unchecked_hex_to_character (&source_p));
2094+
lit_code_point_t code_point = lexer_unchecked_hex_to_character (&source_p);
2095+
if (code_point == (lit_code_point_t) -1)
2096+
{
2097+
return false;
2098+
}
2099+
destination_p += lit_code_point_to_cesu8_bytes (destination_p, code_point);
20872100
continue;
20882101
}
20892102

@@ -2098,6 +2111,7 @@ lexer_convert_ident_to_cesu8 (uint8_t *destination_p, /**< destination string */
20982111

20992112
*destination_p++ = *source_p++;
21002113
} while (destination_p < destination_end_p);
2114+
return true;
21012115
} /* lexer_convert_ident_to_cesu8 */
21022116

21032117
/**
@@ -2130,7 +2144,10 @@ lexer_convert_literal_to_chars (parser_context_t *context_p, /**< context */
21302144

21312145
if (literal_p->type == LEXER_IDENT_LITERAL)
21322146
{
2133-
lexer_convert_ident_to_cesu8 (destination_start_p, literal_p->char_p, literal_p->length);
2147+
if (!lexer_convert_ident_to_cesu8 (destination_start_p, literal_p->char_p, literal_p->length))
2148+
{
2149+
parser_raise_error (context_p, PARSER_ERR_INVALID_UNICODE_ESCAPE_SEQUENCE);
2150+
}
21342151
return destination_start_p;
21352152
}
21362153

@@ -2229,7 +2246,12 @@ lexer_convert_literal_to_chars (parser_context_t *context_p, /**< context */
22292246
if (*source_p == LIT_CHAR_LOWERCASE_X || *source_p == LIT_CHAR_LOWERCASE_U)
22302247
{
22312248
source_p++;
2232-
destination_p += lit_code_point_to_cesu8_bytes (destination_p, lexer_unchecked_hex_to_character (&source_p));
2249+
lit_code_point_t code_point = lexer_unchecked_hex_to_character (&source_p);
2250+
if (code_point == (lit_code_point_t) -1)
2251+
{
2252+
parser_raise_error (context_p, PARSER_ERR_INVALID_UNICODE_ESCAPE_SEQUENCE);
2253+
}
2254+
destination_p += lit_code_point_to_cesu8_bytes (destination_p, code_point);
22332255
continue;
22342256
}
22352257

@@ -3308,6 +3330,10 @@ lexer_compare_identifier_to_chars (const uint8_t *left_p, /**< left identifier *
33083330
{
33093331
left_p += 2;
33103332
lit_code_point_t code_point = lexer_unchecked_hex_to_character (&left_p);
3333+
if (code_point == (lit_code_point_t) -1)
3334+
{
3335+
return false;
3336+
}
33113337

33123338
escape_size = lit_code_point_to_cesu8_bytes (utf8_buf, code_point);
33133339
}

Diff for: jerry-core/parser/js/js-parser-internal.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ void lexer_parse_string (parser_context_t *context_p, lexer_string_options_t opt
748748
void lexer_expect_identifier (parser_context_t *context_p, uint8_t literal_type);
749749
bool lexer_scan_identifier (parser_context_t *context_p, lexer_parse_options_t opts);
750750
void lexer_check_property_modifier (parser_context_t *context_p);
751-
void lexer_convert_ident_to_cesu8 (uint8_t *destination_p, const uint8_t *source_p, prop_length_t length);
751+
bool lexer_convert_ident_to_cesu8 (uint8_t *destination_p, const uint8_t *source_p, prop_length_t length);
752752

753753
const uint8_t *lexer_convert_literal_to_chars (parser_context_t *context_p,
754754
const lexer_lit_location_t *literal_p,

Diff for: tests/jerry/fail/regression-test-issue-5134.js

+15
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+
import{a as "\{{12,34}"

0 commit comments

Comments
 (0)