Skip to content

Commit 34c0810

Browse files
zherczegyichoi
authored andcommitted
Fix escape sequence parsing in lexer_compare_identifier_to_current. (#2409)
Furthermore do not allow escape sequences in object initializer get/set functions. JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent dfc0757 commit 34c0810

File tree

3 files changed

+65
-27
lines changed

3 files changed

+65
-27
lines changed

jerry-core/parser/js/js-lexer.c

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,22 +2222,6 @@ lexer_expect_identifier (parser_context_t *context_p, /**< context */
22222222
parser_raise_error (context_p, PARSER_ERR_IDENTIFIER_EXPECTED);
22232223
} /* lexer_expect_identifier */
22242224

2225-
/**
2226-
* Description of "get" literal string.
2227-
*/
2228-
static const lexer_lit_location_t lexer_get_literal =
2229-
{
2230-
(const uint8_t *) "get", 3, LEXER_IDENT_LITERAL, false
2231-
};
2232-
2233-
/**
2234-
* Description of "set" literal string.
2235-
*/
2236-
static const lexer_lit_location_t lexer_set_literal =
2237-
{
2238-
(const uint8_t *) "set", 3, LEXER_IDENT_LITERAL, false
2239-
};
2240-
22412225
/**
22422226
* Next token must be an identifier.
22432227
*/
@@ -2266,12 +2250,12 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
22662250
if (context_p->source_p < context_p->source_end_p
22672251
&& context_p->source_p[0] != LIT_CHAR_COLON)
22682252
{
2269-
if (lexer_compare_identifier_to_current (context_p, &lexer_get_literal))
2253+
if (lexer_compare_raw_identifier_to_current (context_p, "get", 3))
22702254
{
22712255
context_p->token.type = LEXER_PROPERTY_GETTER;
22722256
return;
22732257
}
2274-
else if (lexer_compare_identifier_to_current (context_p, &lexer_set_literal))
2258+
else if (lexer_compare_raw_identifier_to_current (context_p, "set", 3))
22752259
{
22762260
context_p->token.type = LEXER_PROPERTY_SETTER;
22772261
return;
@@ -2348,11 +2332,11 @@ lexer_scan_identifier (parser_context_t *context_p, /**< context */
23482332
if (context_p->source_p < context_p->source_end_p
23492333
&& context_p->source_p[0] != LIT_CHAR_COLON)
23502334
{
2351-
if (lexer_compare_identifier_to_current (context_p, &lexer_get_literal))
2335+
if (lexer_compare_raw_identifier_to_current (context_p, "get", 3))
23522336
{
23532337
context_p->token.type = LEXER_PROPERTY_GETTER;
23542338
}
2355-
else if (lexer_compare_identifier_to_current (context_p, &lexer_set_literal))
2339+
else if (lexer_compare_raw_identifier_to_current (context_p, "set", 3))
23562340
{
23572341
context_p->token.type = LEXER_PROPERTY_SETTER;
23582342
}
@@ -2376,14 +2360,16 @@ lexer_scan_identifier (parser_context_t *context_p, /**< context */
23762360
} /* lexer_scan_identifier */
23772361

23782362
/**
2379-
* Compares the given identifier to that which is the current token
2380-
* in the parser context.
2363+
* Compares the current identifier in the context to the parameter identifier
2364+
*
2365+
* Note:
2366+
* Escape sequences are allowed.
23812367
*
23822368
* @return true if the input identifiers are the same
23832369
*/
23842370
bool
2385-
lexer_compare_identifier_to_current (parser_context_t *context_p, /**< context */
2386-
const lexer_lit_location_t *right) /**< identifier */
2371+
lexer_compare_identifier_to_current (parser_context_t *context_p, /**< context */
2372+
const lexer_lit_location_t *right) /**< identifier */
23872373
{
23882374
lexer_lit_location_t *left = &context_p->token.lit_location;
23892375
const uint8_t *left_p;
@@ -2424,9 +2410,9 @@ lexer_compare_identifier_to_current (parser_context_t *context_p, /**< co
24242410

24252411
if (*left_p == LIT_CHAR_BACKSLASH && *right_p == LIT_CHAR_BACKSLASH)
24262412
{
2427-
uint16_t left_chr = lexer_hex_to_character (context_p, left_p, 6);
2413+
uint16_t left_chr = lexer_hex_to_character (context_p, left_p + 2, 4);
24282414

2429-
if (left_chr != lexer_hex_to_character (context_p, right_p, 6))
2415+
if (left_chr != lexer_hex_to_character (context_p, right_p + 2, 4))
24302416
{
24312417
return false;
24322418
}
@@ -2446,7 +2432,7 @@ lexer_compare_identifier_to_current (parser_context_t *context_p, /**< co
24462432
right_p = swap_p;
24472433
}
24482434

2449-
utf8_len = lit_char_to_utf8_bytes (utf8_buf, lexer_hex_to_character (context_p, left_p, 6));
2435+
utf8_len = lit_char_to_utf8_bytes (utf8_buf, lexer_hex_to_character (context_p, left_p + 2, 4));
24502436
JERRY_ASSERT (utf8_len > 0);
24512437
count -= utf8_len;
24522438
offset = 0;
@@ -2468,6 +2454,29 @@ lexer_compare_identifier_to_current (parser_context_t *context_p, /**< co
24682454
return true;
24692455
} /* lexer_compare_identifier_to_current */
24702456

2457+
/**
2458+
* Compares the current identifier in the context to the parameter identifier
2459+
*
2460+
* Note:
2461+
* Escape sequences are not allowed.
2462+
*
2463+
* @return true if the input identifiers are the same
2464+
*/
2465+
bool
2466+
lexer_compare_raw_identifier_to_current (parser_context_t *context_p, /**< context */
2467+
const char *right_ident_p, /**< identifier */
2468+
size_t right_ident_length) /**< identifier length */
2469+
{
2470+
lexer_lit_location_t *left = &context_p->token.lit_location;
2471+
2472+
if (left->length != right_ident_length || left->has_escape)
2473+
{
2474+
return 0;
2475+
}
2476+
2477+
return memcmp (left->char_p, right_ident_p, right_ident_length) == 0;
2478+
} /* lexer_compare_raw_identifier_to_current */
2479+
24712480
/**
24722481
* @}
24732482
* @}

jerry-core/parser/js/js-parser-internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ void lexer_convert_push_number_to_push_literal (parser_context_t *context_p);
441441
uint16_t lexer_construct_function_object (parser_context_t *context_p, uint32_t extra_status_flags);
442442
void lexer_construct_regexp_object (parser_context_t *context_p, bool parse_only);
443443
bool lexer_compare_identifier_to_current (parser_context_t *context_p, const lexer_lit_location_t *right);
444+
bool lexer_compare_raw_identifier_to_current (parser_context_t *context_p, const char *right_ident_p,
445+
size_t right_ident_length);
444446

445447
/**
446448
* @}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
// Must be a success.
16+
eval('g\\u0065t: break get')
17+
18+
try {
19+
// Must be a fail.
20+
eval('({ g\\u0065t a() {} })')
21+
assert(false);
22+
} catch (e) {
23+
assert(e instanceof SyntaxError);
24+
}
25+
26+
// Must be a success.
27+
eval('({ g\\u0065t: 5 })')

0 commit comments

Comments
 (0)