mirror of
https://github.com/jerryscript-project/jerryscript.git
synced 2025-12-15 16:29:21 +00:00
Keywords must not contain escape sequences. (#3429)
The ES5.1 standard is unclear about this rule. JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
parent
dc458d29fb
commit
b8bc013fc0
@ -348,6 +348,16 @@ typedef struct
|
|||||||
#define LEXER_KEYWORD_LIST_LENGTH(name) (const uint8_t) (sizeof ((name)) / sizeof ((name)[0]))
|
#define LEXER_KEYWORD_LIST_LENGTH(name) (const uint8_t) (sizeof ((name)) / sizeof ((name)[0]))
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Length of the shortest keyword.
|
||||||
|
*/
|
||||||
|
#define LEXER_KEYWORD_MIN_LENGTH 2
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Length of the longest keyword.
|
||||||
|
*/
|
||||||
|
#define LEXER_KEYWORD_MAX_LENGTH 10
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Keywords with 2 characters.
|
* Keywords with 2 characters.
|
||||||
*/
|
*/
|
||||||
@ -475,6 +485,10 @@ static const keyword_string_t * const keyword_strings_list[] =
|
|||||||
keywords_with_length_10
|
keywords_with_length_10
|
||||||
};
|
};
|
||||||
|
|
||||||
|
JERRY_STATIC_ASSERT (sizeof (keyword_strings_list) / sizeof (const keyword_string_t *)
|
||||||
|
== (LEXER_KEYWORD_MAX_LENGTH - LEXER_KEYWORD_MIN_LENGTH) + 1,
|
||||||
|
keyword_strings_list_size_must_equal_to_keyword_max_length_difference);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List of the keyword groups length.
|
* List of the keyword groups length.
|
||||||
*/
|
*/
|
||||||
@ -510,7 +524,7 @@ lexer_parse_identifier (parser_context_t *context_p, /**< context */
|
|||||||
size_t length = 0;
|
size_t length = 0;
|
||||||
|
|
||||||
context_p->token.type = LEXER_LITERAL;
|
context_p->token.type = LEXER_LITERAL;
|
||||||
context_p->token.literal_is_reserved = false;
|
context_p->token.ident_is_strict_keyword = false;
|
||||||
context_p->token.lit_location.type = LEXER_IDENT_LITERAL;
|
context_p->token.lit_location.type = LEXER_IDENT_LITERAL;
|
||||||
context_p->token.lit_location.has_escape = false;
|
context_p->token.lit_location.has_escape = false;
|
||||||
|
|
||||||
@ -574,21 +588,30 @@ lexer_parse_identifier (parser_context_t *context_p, /**< context */
|
|||||||
|
|
||||||
context_p->source_p = ident_start_p;
|
context_p->source_p = ident_start_p;
|
||||||
context_p->token.column = context_p->column;
|
context_p->token.column = context_p->column;
|
||||||
|
context_p->token.lit_location.char_p = ident_start_p;
|
||||||
|
context_p->token.lit_location.length = (prop_length_t) length;
|
||||||
|
|
||||||
if (length > PARSER_MAXIMUM_IDENT_LENGTH)
|
if (length > PARSER_MAXIMUM_IDENT_LENGTH)
|
||||||
{
|
{
|
||||||
parser_raise_error (context_p, PARSER_ERR_IDENTIFIER_TOO_LONG);
|
parser_raise_error (context_p, PARSER_ERR_IDENTIFIER_TOO_LONG);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check keywords (Only if there is no \u escape sequence in the pattern). */
|
/* Check keywords. */
|
||||||
if (check_keywords
|
if (check_keywords
|
||||||
&& !context_p->token.lit_location.has_escape
|
&& (length >= LEXER_KEYWORD_MIN_LENGTH && length <= LEXER_KEYWORD_MAX_LENGTH))
|
||||||
&& (length >= 2 && length <= 10))
|
|
||||||
{
|
{
|
||||||
const keyword_string_t *keyword_list_p = keyword_strings_list[length - 2];
|
uint8_t buffer_p[LEXER_KEYWORD_MAX_LENGTH];
|
||||||
|
|
||||||
|
if (JERRY_UNLIKELY (context_p->token.lit_location.has_escape))
|
||||||
|
{
|
||||||
|
lexer_convert_ident_to_cesu8 (ident_start_p, buffer_p, (prop_length_t) length);
|
||||||
|
ident_start_p = buffer_p;
|
||||||
|
}
|
||||||
|
|
||||||
|
const keyword_string_t *keyword_list_p = keyword_strings_list[length - LEXER_KEYWORD_MIN_LENGTH];
|
||||||
|
|
||||||
int start = 0;
|
int start = 0;
|
||||||
int end = keyword_lengths_list[length - 2];
|
int end = keyword_lengths_list[length - LEXER_KEYWORD_MIN_LENGTH];
|
||||||
int middle = end / 2;
|
int middle = end / 2;
|
||||||
|
|
||||||
do
|
do
|
||||||
@ -607,6 +630,11 @@ lexer_parse_identifier (parser_context_t *context_p, /**< context */
|
|||||||
#if ENABLED (JERRY_ES2015)
|
#if ENABLED (JERRY_ES2015)
|
||||||
if (keyword_p->type == LEXER_KEYW_YIELD && (context_p->status_flags & PARSER_IS_GENERATOR_FUNCTION))
|
if (keyword_p->type == LEXER_KEYW_YIELD && (context_p->status_flags & PARSER_IS_GENERATOR_FUNCTION))
|
||||||
{
|
{
|
||||||
|
if (ident_start_p == buffer_p)
|
||||||
|
{
|
||||||
|
parser_raise_error (context_p, PARSER_ERR_INVALID_KEYWORD);
|
||||||
|
}
|
||||||
|
|
||||||
if (context_p->status_flags & PARSER_DISALLOW_YIELD)
|
if (context_p->status_flags & PARSER_DISALLOW_YIELD)
|
||||||
{
|
{
|
||||||
parser_raise_error (context_p, PARSER_ERR_YIELD_NOT_ALLOWED);
|
parser_raise_error (context_p, PARSER_ERR_YIELD_NOT_ALLOWED);
|
||||||
@ -622,10 +650,15 @@ lexer_parse_identifier (parser_context_t *context_p, /**< context */
|
|||||||
parser_raise_error (context_p, PARSER_ERR_STRICT_IDENT_NOT_ALLOWED);
|
parser_raise_error (context_p, PARSER_ERR_STRICT_IDENT_NOT_ALLOWED);
|
||||||
}
|
}
|
||||||
|
|
||||||
context_p->token.literal_is_reserved = true;
|
context_p->token.ident_is_strict_keyword = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ident_start_p == buffer_p)
|
||||||
|
{
|
||||||
|
parser_raise_error (context_p, PARSER_ERR_INVALID_KEYWORD);
|
||||||
|
}
|
||||||
|
|
||||||
context_p->token.type = (uint8_t) keyword_p->type;
|
context_p->token.type = (uint8_t) keyword_p->type;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -646,13 +679,6 @@ lexer_parse_identifier (parser_context_t *context_p, /**< context */
|
|||||||
while (start < end);
|
while (start < end);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (context_p->token.type == LEXER_LITERAL)
|
|
||||||
{
|
|
||||||
/* Fill literal data. */
|
|
||||||
context_p->token.lit_location.char_p = ident_start_p;
|
|
||||||
context_p->token.lit_location.length = (prop_length_t) length;
|
|
||||||
}
|
|
||||||
|
|
||||||
context_p->source_p = source_p;
|
context_p->source_p = source_p;
|
||||||
context_p->column = column;
|
context_p->column = column;
|
||||||
} /* lexer_parse_identifier */
|
} /* lexer_parse_identifier */
|
||||||
@ -964,7 +990,7 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
|
|||||||
size_t length;
|
size_t length;
|
||||||
|
|
||||||
context_p->token.type = LEXER_LITERAL;
|
context_p->token.type = LEXER_LITERAL;
|
||||||
context_p->token.literal_is_reserved = false;
|
context_p->token.ident_is_strict_keyword = false;
|
||||||
context_p->token.extra_value = LEXER_NUMBER_DECIMAL;
|
context_p->token.extra_value = LEXER_NUMBER_DECIMAL;
|
||||||
context_p->token.lit_location.char_p = source_p;
|
context_p->token.lit_location.char_p = source_p;
|
||||||
context_p->token.lit_location.type = LEXER_NUMBER_LITERAL;
|
context_p->token.lit_location.type = LEXER_NUMBER_LITERAL;
|
||||||
@ -1688,9 +1714,9 @@ lexer_process_char_literal (parser_context_t *context_p, /**< context */
|
|||||||
* Convert an ident with escapes to a utf8 string.
|
* Convert an ident with escapes to a utf8 string.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
lexer_convert_ident_to_utf8 (const uint8_t *source_p, /**< source string */
|
lexer_convert_ident_to_cesu8 (const uint8_t *source_p, /**< source string */
|
||||||
uint8_t *destination_p, /**< destination string */
|
uint8_t *destination_p, /**< destination string */
|
||||||
prop_length_t length) /**< length of destination string */
|
prop_length_t length) /**< length of destination string */
|
||||||
{
|
{
|
||||||
const uint8_t *destination_end_p = destination_p + length;
|
const uint8_t *destination_end_p = destination_p + length;
|
||||||
|
|
||||||
@ -1712,7 +1738,7 @@ lexer_convert_ident_to_utf8 (const uint8_t *source_p, /**< source string */
|
|||||||
*destination_p++ = *source_p++;
|
*destination_p++ = *source_p++;
|
||||||
}
|
}
|
||||||
while (destination_p < destination_end_p);
|
while (destination_p < destination_end_p);
|
||||||
} /* lexer_convert_ident_to_utf8 */
|
} /* lexer_convert_ident_to_cesu8 */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a literal object from an identifier.
|
* Construct a literal object from an identifier.
|
||||||
@ -1748,7 +1774,7 @@ lexer_construct_literal_object (parser_context_t *context_p, /**< context */
|
|||||||
|
|
||||||
if (literal_p->type == LEXER_IDENT_LITERAL)
|
if (literal_p->type == LEXER_IDENT_LITERAL)
|
||||||
{
|
{
|
||||||
lexer_convert_ident_to_utf8 (source_p, destination_start_p, literal_p->length);
|
lexer_convert_ident_to_cesu8 (source_p, destination_start_p, literal_p->length);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -2412,7 +2438,7 @@ lexer_construct_regexp_object (parser_context_t *context_p, /**< context */
|
|||||||
literal_p->u.bytecode_p = (ecma_compiled_code_t *) re_bytecode_p;
|
literal_p->u.bytecode_p = (ecma_compiled_code_t *) re_bytecode_p;
|
||||||
|
|
||||||
context_p->token.type = LEXER_LITERAL;
|
context_p->token.type = LEXER_LITERAL;
|
||||||
context_p->token.literal_is_reserved = false;
|
context_p->token.ident_is_strict_keyword = false;
|
||||||
context_p->token.lit_location.type = LEXER_REGEXP_LITERAL;
|
context_p->token.lit_location.type = LEXER_REGEXP_LITERAL;
|
||||||
|
|
||||||
context_p->lit_object.literal_p = literal_p;
|
context_p->lit_object.literal_p = literal_p;
|
||||||
@ -2479,7 +2505,7 @@ lexer_expect_identifier (parser_context_t *context_p, /**< context */
|
|||||||
/* When parsing default exports for modules, it is not required by functions or classes to have identifiers.
|
/* When parsing default exports for modules, it is not required by functions or classes to have identifiers.
|
||||||
* In this case we use a synthetic name for them. */
|
* In this case we use a synthetic name for them. */
|
||||||
context_p->token.type = LEXER_LITERAL;
|
context_p->token.type = LEXER_LITERAL;
|
||||||
context_p->token.literal_is_reserved = false;
|
context_p->token.ident_is_strict_keyword = false;
|
||||||
context_p->token.lit_location.type = LEXER_IDENT_LITERAL;
|
context_p->token.lit_location.type = LEXER_IDENT_LITERAL;
|
||||||
context_p->token.lit_location.has_escape = false;
|
context_p->token.lit_location.has_escape = false;
|
||||||
lexer_construct_literal_object (context_p, &lexer_default_literal, literal_type);
|
lexer_construct_literal_object (context_p, &lexer_default_literal, literal_type);
|
||||||
|
|||||||
@ -288,8 +288,7 @@ typedef struct
|
|||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
uint8_t type; /**< token type */
|
uint8_t type; /**< token type */
|
||||||
uint8_t literal_is_reserved; /**< future reserved keyword
|
uint8_t ident_is_strict_keyword; /**< identifier is strict reserved keyword */
|
||||||
* (when char_literal.type is LEXER_IDENT_LITERAL) */
|
|
||||||
uint8_t extra_value; /**< helper value for different purposes */
|
uint8_t extra_value; /**< helper value for different purposes */
|
||||||
uint8_t flags; /**< flag bits for the current token */
|
uint8_t flags; /**< flag bits for the current token */
|
||||||
parser_line_counter_t line; /**< token start line */
|
parser_line_counter_t line; /**< token start line */
|
||||||
|
|||||||
@ -1054,7 +1054,7 @@ parser_parse_function_expression (parser_context_t *context_p, /**< context */
|
|||||||
}
|
}
|
||||||
#endif /* ENABLED (JERRY_DEBUGGER) */
|
#endif /* ENABLED (JERRY_DEBUGGER) */
|
||||||
|
|
||||||
if (context_p->token.literal_is_reserved
|
if (context_p->token.ident_is_strict_keyword
|
||||||
|| context_p->lit_object.type != LEXER_LITERAL_OBJECT_ANY)
|
|| context_p->lit_object.type != LEXER_LITERAL_OBJECT_ANY)
|
||||||
{
|
{
|
||||||
status_flags |= PARSER_HAS_NON_STRICT_ARG;
|
status_flags |= PARSER_HAS_NON_STRICT_ARG;
|
||||||
|
|||||||
@ -637,7 +637,7 @@ void lexer_parse_string (parser_context_t *context_p);
|
|||||||
void lexer_expect_identifier (parser_context_t *context_p, uint8_t literal_type);
|
void lexer_expect_identifier (parser_context_t *context_p, uint8_t literal_type);
|
||||||
void lexer_scan_identifier (parser_context_t *context_p, uint32_t ident_opts);
|
void lexer_scan_identifier (parser_context_t *context_p, uint32_t ident_opts);
|
||||||
ecma_char_t lexer_hex_to_character (parser_context_t *context_p, const uint8_t *source_p, int length);
|
ecma_char_t lexer_hex_to_character (parser_context_t *context_p, const uint8_t *source_p, int length);
|
||||||
void lexer_convert_ident_to_utf8 (const uint8_t *source_p, uint8_t *destination_p, prop_length_t length);
|
void lexer_convert_ident_to_cesu8 (const uint8_t *source_p, uint8_t *destination_p, prop_length_t length);
|
||||||
void lexer_expect_object_literal_id (parser_context_t *context_p, uint32_t ident_opts);
|
void lexer_expect_object_literal_id (parser_context_t *context_p, uint32_t ident_opts);
|
||||||
void lexer_construct_literal_object (parser_context_t *context_p, const lexer_lit_location_t *literal_p,
|
void lexer_construct_literal_object (parser_context_t *context_p, const lexer_lit_location_t *literal_p,
|
||||||
uint8_t literal_type);
|
uint8_t literal_type);
|
||||||
|
|||||||
@ -367,7 +367,7 @@ parser_module_parse_export_clause (parser_context_t *context_p) /**< parser cont
|
|||||||
/* 15.2.3.1 The referenced binding cannot be a reserved word. */
|
/* 15.2.3.1 The referenced binding cannot be a reserved word. */
|
||||||
if (context_p->token.type != LEXER_LITERAL
|
if (context_p->token.type != LEXER_LITERAL
|
||||||
|| context_p->token.lit_location.type != LEXER_IDENT_LITERAL
|
|| context_p->token.lit_location.type != LEXER_IDENT_LITERAL
|
||||||
|| context_p->token.literal_is_reserved)
|
|| context_p->token.ident_is_strict_keyword)
|
||||||
{
|
{
|
||||||
parser_raise_error (context_p, PARSER_ERR_IDENTIFIER_EXPECTED);
|
parser_raise_error (context_p, PARSER_ERR_IDENTIFIER_EXPECTED);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -852,6 +852,10 @@ parser_error_to_string (parser_error_t error) /**< error code */
|
|||||||
{
|
{
|
||||||
return "Character cannot be part of an identifier.";
|
return "Character cannot be part of an identifier.";
|
||||||
}
|
}
|
||||||
|
case PARSER_ERR_INVALID_KEYWORD:
|
||||||
|
{
|
||||||
|
return "Escape sequences are not allowed in keywords.";
|
||||||
|
}
|
||||||
case PARSER_ERR_INVALID_NUMBER:
|
case PARSER_ERR_INVALID_NUMBER:
|
||||||
{
|
{
|
||||||
return "Invalid number.";
|
return "Invalid number.";
|
||||||
|
|||||||
@ -1729,7 +1729,7 @@ parser_parse_function_arguments (parser_context_t *context_p, /**< context */
|
|||||||
&context_p->token.lit_location,
|
&context_p->token.lit_location,
|
||||||
LEXER_IDENT_LITERAL);
|
LEXER_IDENT_LITERAL);
|
||||||
|
|
||||||
if (context_p->token.literal_is_reserved
|
if (context_p->token.ident_is_strict_keyword
|
||||||
|| context_p->lit_object.type != LEXER_LITERAL_OBJECT_ANY)
|
|| context_p->lit_object.type != LEXER_LITERAL_OBJECT_ANY)
|
||||||
{
|
{
|
||||||
context_p->status_flags |= PARSER_HAS_NON_STRICT_ARG;
|
context_p->status_flags |= PARSER_HAS_NON_STRICT_ARG;
|
||||||
|
|||||||
@ -48,6 +48,7 @@ typedef enum
|
|||||||
PARSER_ERR_INVALID_UNICODE_ESCAPE_SEQUENCE, /**< invalid unicode escape sequence */
|
PARSER_ERR_INVALID_UNICODE_ESCAPE_SEQUENCE, /**< invalid unicode escape sequence */
|
||||||
PARSER_ERR_INVALID_IDENTIFIER_START, /**< character cannot be start of an identifier */
|
PARSER_ERR_INVALID_IDENTIFIER_START, /**< character cannot be start of an identifier */
|
||||||
PARSER_ERR_INVALID_IDENTIFIER_PART, /**< character cannot be part of an identifier */
|
PARSER_ERR_INVALID_IDENTIFIER_PART, /**< character cannot be part of an identifier */
|
||||||
|
PARSER_ERR_INVALID_KEYWORD, /**< escape sequences are not allowed in keywords */
|
||||||
|
|
||||||
PARSER_ERR_INVALID_NUMBER, /**< invalid number literal */
|
PARSER_ERR_INVALID_NUMBER, /**< invalid number literal */
|
||||||
PARSER_ERR_MISSING_EXPONENT, /**< missing exponent */
|
PARSER_ERR_MISSING_EXPONENT, /**< missing exponent */
|
||||||
|
|||||||
@ -1147,7 +1147,7 @@ scanner_scope_find_let_declaration (parser_context_t *context_p, /**< context */
|
|||||||
{
|
{
|
||||||
uint8_t *destination_p = (uint8_t *) scanner_malloc (context_p, literal_p->length);
|
uint8_t *destination_p = (uint8_t *) scanner_malloc (context_p, literal_p->length);
|
||||||
|
|
||||||
lexer_convert_ident_to_utf8 (literal_p->char_p, destination_p, literal_p->length);
|
lexer_convert_ident_to_cesu8 (literal_p->char_p, destination_p, literal_p->length);
|
||||||
|
|
||||||
name_p = ecma_new_ecma_string_from_utf8 (destination_p, literal_p->length);
|
name_p = ecma_new_ecma_string_from_utf8 (destination_p, literal_p->length);
|
||||||
scanner_free (destination_p, literal_p->length);
|
scanner_free (destination_p, literal_p->length);
|
||||||
|
|||||||
47
tests/jerry/keyword.js
Normal file
47
tests/jerry/keyword.js
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
// Copyright JS Foundation and other contributors, http://js.foundation
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
function check_syntax_error(code)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
eval(code)
|
||||||
|
assert(false)
|
||||||
|
} catch (e) {
|
||||||
|
assert(e instanceof SyntaxError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function check_strict_syntax_error(code)
|
||||||
|
{
|
||||||
|
"use strict"
|
||||||
|
|
||||||
|
try {
|
||||||
|
eval(code)
|
||||||
|
assert(false)
|
||||||
|
} catch (e) {
|
||||||
|
assert(e instanceof SyntaxError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
check_syntax_error("d\\u006f {} while (false)")
|
||||||
|
check_syntax_error("\\u0076\\u0061\\u0072 var = 5")
|
||||||
|
check_syntax_error("wit\\u0068 ({}) {}")
|
||||||
|
check_syntax_error("\\u0066alse")
|
||||||
|
check_syntax_error("type\\006ff 3.14")
|
||||||
|
check_syntax_error("try {} fin\\u0061lly {}")
|
||||||
|
check_syntax_error("f\\u0075nction f() {}")
|
||||||
|
check_syntax_error("a instanc\\u0065of b")
|
||||||
|
|
||||||
|
check_strict_syntax_error("\\u006c\\u0065\\u0074 _let = 5");
|
||||||
|
check_strict_syntax_error("\\u0070rotecte\\u0064");
|
||||||
Loading…
x
Reference in New Issue
Block a user