Skip to content

Commit 96edec1

Browse files
zherczegrerobika
authored andcommitted
Improve expected identifier checks. (#3064)
Checks for "of" or "from" does not accept quoted strings. JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
1 parent 086c4eb commit 96edec1

File tree

8 files changed

+72
-51
lines changed

8 files changed

+72
-51
lines changed

jerry-core/parser/js/common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
*/
4848
typedef enum
4949
{
50+
/* The LEXER_IS_IDENT_OR_STRING macro must be updated if the order is changed. */
5051
LEXER_IDENT_LITERAL = 0, /**< identifier literal */
5152
LEXER_STRING_LITERAL = 1, /**< string literal */
5253
LEXER_NUMBER_LITERAL = 2, /**< number literal */
@@ -56,6 +57,11 @@ typedef enum
5657
used by the byte code generator. */
5758
} lexer_literal_type_t;
5859

60+
/**
61+
* Checks whether the literal type is identifier or string.
62+
*/
63+
#define LEXER_IS_IDENT_OR_STRING(literal_type) ((literal_type) <= LEXER_STRING_LITERAL)
64+
5965
/**
6066
* Flag bits for status_flags member of lexer_literal_t.
6167
*/

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

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,12 +2331,12 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
23312331
if (context_p->source_p < context_p->source_end_p
23322332
&& context_p->source_p[0] != LIT_CHAR_COLON)
23332333
{
2334-
if (lexer_compare_raw_identifier_to_current (context_p, "get", 3))
2334+
if (lexer_compare_literal_to_string (context_p, "get", 3))
23352335
{
23362336
context_p->token.type = LEXER_PROPERTY_GETTER;
23372337
return;
23382338
}
2339-
else if (lexer_compare_raw_identifier_to_current (context_p, "set", 3))
2339+
else if (lexer_compare_literal_to_string (context_p, "set", 3))
23402340
{
23412341
context_p->token.type = LEXER_PROPERTY_SETTER;
23422342
return;
@@ -2345,8 +2345,7 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
23452345
}
23462346

23472347
#if ENABLED (JERRY_ES2015_CLASS)
2348-
if (is_class_method
2349-
&& lexer_compare_raw_identifier_to_current (context_p, "static", 6))
2348+
if (is_class_method && lexer_compare_literal_to_string (context_p, "static", 6))
23502349
{
23512350
context_p->token.type = LEXER_KEYW_STATIC;
23522351
return;
@@ -2406,8 +2405,7 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
24062405
if (create_literal_object)
24072406
{
24082407
#if ENABLED (JERRY_ES2015_CLASS)
2409-
if (is_class_method
2410-
&& lexer_compare_raw_identifier_to_current (context_p, "constructor", 11))
2408+
if (is_class_method && lexer_compare_literal_to_string (context_p, "constructor", 11))
24112409
{
24122410
context_p->token.type = LEXER_CLASS_CONSTRUCTOR;
24132411
return;
@@ -2447,11 +2445,11 @@ lexer_scan_identifier (parser_context_t *context_p, /**< context */
24472445
if (context_p->source_p < context_p->source_end_p
24482446
&& context_p->source_p[0] != LIT_CHAR_COLON)
24492447
{
2450-
if (lexer_compare_raw_identifier_to_current (context_p, "get", 3))
2448+
if (lexer_compare_literal_to_string (context_p, "get", 3))
24512449
{
24522450
context_p->token.type = LEXER_PROPERTY_GETTER;
24532451
}
2454-
else if (lexer_compare_raw_identifier_to_current (context_p, "set", 3))
2452+
else if (lexer_compare_literal_to_string (context_p, "set", 3))
24552453
{
24562454
context_p->token.type = LEXER_PROPERTY_SETTER;
24572455
}
@@ -2573,27 +2571,46 @@ lexer_compare_identifier_to_current (parser_context_t *context_p, /**< context *
25732571
} /* lexer_compare_identifier_to_current */
25742572

25752573
/**
2576-
* Compares the current identifier in the context to the parameter identifier
2574+
* Compares the current identifier to an expected identifier.
25772575
*
25782576
* Note:
25792577
* Escape sequences are not allowed.
25802578
*
2581-
* @return true if the input identifiers are the same
2579+
* @return true if they are the same, false otherwise
25822580
*/
2583-
bool
2584-
lexer_compare_raw_identifier_to_current (parser_context_t *context_p, /**< context */
2585-
const char *right_ident_p, /**< identifier */
2586-
size_t right_ident_length) /**< identifier length */
2581+
inline bool JERRY_ATTR_ALWAYS_INLINE
2582+
lexer_compare_literal_to_identifier (parser_context_t *context_p, /**< context */
2583+
const char *identifier_p, /**< identifier */
2584+
size_t identifier_length) /**< identifier length */
25872585
{
2588-
lexer_lit_location_t *left_ident_p = &context_p->token.lit_location;
2589-
2590-
if (left_ident_p->length != right_ident_length || left_ident_p->has_escape)
2591-
{
2592-
return 0;
2593-
}
2586+
/* Checking has_escape is unnecessary because memcmp will fail if escape sequences are present. */
2587+
return (context_p->token.type == LEXER_LITERAL
2588+
&& context_p->token.lit_location.type == LEXER_IDENT_LITERAL
2589+
&& context_p->token.lit_location.length == identifier_length
2590+
&& memcmp (context_p->token.lit_location.char_p, identifier_p, identifier_length) == 0);
2591+
} /* lexer_compare_literal_to_identifier */
25942592

2595-
return memcmp (left_ident_p->char_p, right_ident_p, right_ident_length) == 0;
2596-
} /* lexer_compare_raw_identifier_to_current */
2593+
/**
2594+
* Compares the current identifier or string to an expected string.
2595+
*
2596+
* Note:
2597+
* Escape sequences are not allowed.
2598+
*
2599+
* @return true if they are the same, false otherwise
2600+
*/
2601+
inline bool JERRY_ATTR_ALWAYS_INLINE
2602+
lexer_compare_literal_to_string (parser_context_t *context_p, /**< context */
2603+
const char *string_p, /**< string */
2604+
size_t string_length) /**< string length */
2605+
{
2606+
JERRY_ASSERT (context_p->token.type == LEXER_LITERAL
2607+
&& (context_p->token.lit_location.type == LEXER_IDENT_LITERAL
2608+
|| context_p->token.lit_location.type == LEXER_STRING_LITERAL));
2609+
2610+
/* Checking has_escape is unnecessary because memcmp will fail if escape sequences are present. */
2611+
return (context_p->token.lit_location.length == string_length
2612+
&& memcmp (context_p->token.lit_location.char_p, string_p, string_length) == 0);
2613+
} /* lexer_compare_literal_to_string */
25972614

25982615
/**
25992616
* Convert binary lvalue token to binary token

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,8 @@ parser_parse_class_literal (parser_context_t *context_p) /**< context */
442442
is_computed = true;
443443
}
444444
else if (!(status_flags & PARSER_CLASS_STATIC_FUNCTION)
445-
&& lexer_compare_raw_identifier_to_current (context_p, "constructor", 11))
445+
&& LEXER_IS_IDENT_OR_STRING (context_p->token.lit_location.type)
446+
&& lexer_compare_literal_to_string (context_p, "constructor", 11))
446447
{
447448
parser_raise_error (context_p, PARSER_ERR_CLASS_CONSTRUCTOR_AS_ACCESSOR);
448449
}
@@ -538,7 +539,8 @@ parser_parse_class_literal (parser_context_t *context_p) /**< context */
538539
is_computed = true;
539540
}
540541
else if ((status_flags & PARSER_CLASS_STATIC_FUNCTION)
541-
&& lexer_compare_raw_identifier_to_current (context_p, "prototype", 9))
542+
&& LEXER_IS_IDENT_OR_STRING (context_p->token.lit_location.type)
543+
&& lexer_compare_literal_to_string (context_p, "prototype", 9))
542544
{
543545
parser_raise_error (context_p, PARSER_ERR_CLASS_STATIC_PROTOTYPE);
544546
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,9 @@ void lexer_convert_push_number_to_push_literal (parser_context_t *context_p);
521521
uint16_t lexer_construct_function_object (parser_context_t *context_p, uint32_t extra_status_flags);
522522
void lexer_construct_regexp_object (parser_context_t *context_p, bool parse_only);
523523
bool lexer_compare_identifier_to_current (parser_context_t *context_p, const lexer_lit_location_t *right_ident_p);
524-
bool lexer_compare_raw_identifier_to_current (parser_context_t *context_p, const char *right_ident_p,
525-
size_t right_ident_length);
524+
bool lexer_compare_literal_to_identifier (parser_context_t *context_p, const char *identifier_p,
525+
size_t identifier_length);
526+
bool lexer_compare_literal_to_string (parser_context_t *context_p, const char *string_p, size_t string_length);
526527
uint8_t lexer_convert_binary_lvalue_token_to_binary (uint8_t token);
527528

528529
/**

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,7 @@ parser_module_parse_export_clause (parser_context_t *context_p) /**< parser cont
362362
uint16_t export_name_index = PARSER_MAXIMUM_NUMBER_OF_LITERALS;
363363

364364
lexer_next_token (context_p);
365-
if (context_p->token.type == LEXER_LITERAL
366-
&& lexer_compare_raw_identifier_to_current (context_p, "as", 2))
365+
if (lexer_compare_literal_to_identifier (context_p, "as", 2))
367366
{
368367
lexer_next_token (context_p);
369368

@@ -415,8 +414,7 @@ parser_module_parse_export_clause (parser_context_t *context_p) /**< parser cont
415414
lexer_next_token (context_p);
416415
}
417416

418-
if (context_p->token.type == LEXER_LITERAL
419-
&& lexer_compare_raw_identifier_to_current (context_p, "from", 4))
417+
if (lexer_compare_literal_to_identifier (context_p, "from", 4))
420418
{
421419
parser_raise_error (context_p, PARSER_ERR_RIGHT_BRACE_EXPECTED);
422420
}
@@ -455,8 +453,7 @@ parser_module_parse_import_clause (parser_context_t *context_p) /**< parser cont
455453
uint16_t local_name_index = PARSER_MAXIMUM_NUMBER_OF_LITERALS;
456454

457455
lexer_next_token (context_p);
458-
if (context_p->token.type == LEXER_LITERAL
459-
&& lexer_compare_raw_identifier_to_current (context_p, "as", 2))
456+
if (lexer_compare_literal_to_identifier (context_p, "as", 2))
460457
{
461458
lexer_next_token (context_p);
462459

@@ -508,8 +505,7 @@ parser_module_parse_import_clause (parser_context_t *context_p) /**< parser cont
508505
lexer_next_token (context_p);
509506
}
510507

511-
if (context_p->token.type == LEXER_LITERAL
512-
&& lexer_compare_raw_identifier_to_current (context_p, "from", 4))
508+
if (lexer_compare_literal_to_identifier (context_p, "from", 4))
513509
{
514510
parser_raise_error (context_p, PARSER_ERR_RIGHT_BRACE_EXPECTED);
515511
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ parser_scan_until (parser_context_t *context_p, /**< context */
720720
context_p->token.type = LEXER_KEYW_IN;
721721
return;
722722
}
723-
else if (type == LEXER_LITERAL && lexer_compare_raw_identifier_to_current (context_p, "of", 2))
723+
else if (lexer_compare_literal_to_identifier (context_p, "of", 2))
724724
{
725725
parser_stack_pop_uint8 (context_p);
726726
context_p->token.type = LEXER_LITERAL_OF;
@@ -788,13 +788,13 @@ parser_scan_until (parser_context_t *context_p, /**< context */
788788
break;
789789
}
790790

791-
if (lexer_compare_raw_identifier_to_current (context_p, "static", 6))
791+
if (lexer_compare_literal_to_identifier (context_p, "static", 6))
792792
{
793793
lexer_next_token (context_p);
794794
}
795795

796-
if (lexer_compare_raw_identifier_to_current (context_p, "get", 3)
797-
|| lexer_compare_raw_identifier_to_current (context_p, "set", 3))
796+
if (lexer_compare_literal_to_identifier (context_p, "get", 3)
797+
|| lexer_compare_literal_to_identifier (context_p, "set", 3))
798798
{
799799
lexer_next_token (context_p);
800800
}

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,12 +1884,6 @@ parser_parse_import_statement (parser_context_t *context_p) /**< parser context
18841884

18851885
lexer_next_token (context_p);
18861886

1887-
if (context_p->token.type != LEXER_COMMA
1888-
&& !lexer_compare_raw_identifier_to_current (context_p, "from", 4))
1889-
{
1890-
parser_raise_error (context_p, PARSER_ERR_FROM_COMMA_EXPECTED);
1891-
}
1892-
18931887
if (context_p->token.type == LEXER_COMMA)
18941888
{
18951889
lexer_next_token (context_p);
@@ -1899,14 +1893,17 @@ parser_parse_import_statement (parser_context_t *context_p) /**< parser context
18991893
parser_raise_error (context_p, PARSER_ERR_LEFT_BRACE_MULTIPLY_EXPECTED);
19001894
}
19011895
}
1896+
else if (!lexer_compare_literal_to_identifier (context_p, "from", 4))
1897+
{
1898+
parser_raise_error (context_p, PARSER_ERR_FROM_COMMA_EXPECTED);
1899+
}
19021900
}
19031901

19041902
if (context_p->token.type == LEXER_MULTIPLY)
19051903
{
19061904
/* NameSpaceImport*/
19071905
lexer_next_token (context_p);
1908-
if (context_p->token.type != LEXER_LITERAL
1909-
|| !lexer_compare_raw_identifier_to_current (context_p, "as", 2))
1906+
if (!lexer_compare_literal_to_identifier (context_p, "as", 2))
19101907
{
19111908
parser_raise_error (context_p, PARSER_ERR_AS_EXPECTED);
19121909
}
@@ -1942,7 +1939,7 @@ parser_parse_import_statement (parser_context_t *context_p) /**< parser context
19421939
parser_module_parse_import_clause (context_p);
19431940
}
19441941

1945-
if (context_p->token.type != LEXER_LITERAL || !lexer_compare_raw_identifier_to_current (context_p, "from", 4))
1942+
if (!lexer_compare_literal_to_identifier (context_p, "from", 4))
19461943
{
19471944
parser_raise_error (context_p, PARSER_ERR_FROM_EXPECTED);
19481945
}
@@ -2035,8 +2032,7 @@ parser_parse_export_statement (parser_context_t *context_p) /**< context */
20352032
case LEXER_MULTIPLY:
20362033
{
20372034
lexer_next_token (context_p);
2038-
if (!(context_p->token.type == LEXER_LITERAL
2039-
&& lexer_compare_raw_identifier_to_current (context_p, "from", 4)))
2035+
if (!lexer_compare_literal_to_identifier (context_p, "from", 4))
20402036
{
20412037
parser_raise_error (context_p, PARSER_ERR_FROM_EXPECTED);
20422038
}
@@ -2106,8 +2102,7 @@ parser_parse_export_statement (parser_context_t *context_p) /**< context */
21062102
{
21072103
parser_module_parse_export_clause (context_p);
21082104

2109-
if (context_p->token.type == LEXER_LITERAL
2110-
&& lexer_compare_raw_identifier_to_current (context_p, "from", 4))
2105+
if (lexer_compare_literal_to_identifier (context_p, "from", 4))
21112106
{
21122107
lexer_next_token (context_p);
21132108
parser_module_handle_module_specifier (context_p);

tests/jerry/es2015/for-of.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ var forOf =
5252
" obj[prop] += 4;"
5353
parse (forOf)
5454

55+
var forOf =
56+
"for (var a \"of\" []) {}"
57+
parse (forOf)
58+
5559
checkError(5)
5660

5761
var obj = {}

0 commit comments

Comments
 (0)