@@ -147,7 +147,7 @@ lexer_hex_in_braces_to_code_point (const uint8_t *source_p, /**< current source
147
147
/**
148
148
* Parse hexadecimal character sequence
149
149
*
150
- * @return character value
150
+ * @return character value (-1 if the escape sequence is invalid)
151
151
*/
152
152
static lit_code_point_t
153
153
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
174
174
}
175
175
else
176
176
{
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
+ }
179
182
180
183
result += LEXER_TO_ASCII_LOWERCASE (byte ) - (LIT_CHAR_LOWERCASE_A - 10 );
181
184
}
182
185
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
+ }
184
190
185
191
if (length == 0 )
186
192
{
@@ -2068,8 +2074,10 @@ lexer_scan_private_identifier (parser_context_t *context_p) /**< context */
2068
2074
2069
2075
/**
2070
2076
* Convert an ident with escapes to a utf8 string.
2077
+ *
2078
+ * @return false if source contains invalid unicode escape sequence, true otherwise
2071
2079
*/
2072
- void
2080
+ bool
2073
2081
lexer_convert_ident_to_cesu8 (uint8_t * destination_p , /**< destination string */
2074
2082
const uint8_t * source_p , /**< source string */
2075
2083
prop_length_t length ) /**< length of destination string */
@@ -2083,7 +2091,12 @@ lexer_convert_ident_to_cesu8 (uint8_t *destination_p, /**< destination string */
2083
2091
if (* source_p == LIT_CHAR_BACKSLASH )
2084
2092
{
2085
2093
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 );
2087
2100
continue ;
2088
2101
}
2089
2102
@@ -2098,6 +2111,7 @@ lexer_convert_ident_to_cesu8 (uint8_t *destination_p, /**< destination string */
2098
2111
2099
2112
* destination_p ++ = * source_p ++ ;
2100
2113
} while (destination_p < destination_end_p );
2114
+ return true;
2101
2115
} /* lexer_convert_ident_to_cesu8 */
2102
2116
2103
2117
/**
@@ -2130,7 +2144,10 @@ lexer_convert_literal_to_chars (parser_context_t *context_p, /**< context */
2130
2144
2131
2145
if (literal_p -> type == LEXER_IDENT_LITERAL )
2132
2146
{
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
+ }
2134
2151
return destination_start_p ;
2135
2152
}
2136
2153
@@ -2229,7 +2246,12 @@ lexer_convert_literal_to_chars (parser_context_t *context_p, /**< context */
2229
2246
if (* source_p == LIT_CHAR_LOWERCASE_X || * source_p == LIT_CHAR_LOWERCASE_U )
2230
2247
{
2231
2248
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 );
2233
2255
continue ;
2234
2256
}
2235
2257
@@ -3308,6 +3330,10 @@ lexer_compare_identifier_to_chars (const uint8_t *left_p, /**< left identifier *
3308
3330
{
3309
3331
left_p += 2 ;
3310
3332
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
+ }
3311
3337
3312
3338
escape_size = lit_code_point_to_cesu8_bytes (utf8_buf , code_point );
3313
3339
}
0 commit comments