@@ -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 */
152152static lit_code_point_t
153153lexer_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
20732081lexer_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 }
0 commit comments