Skip to content

Fix escape sequence parsing in lexer_compare_identifier_to_current. #2409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 36 additions & 27 deletions jerry-core/parser/js/js-lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2222,22 +2222,6 @@ lexer_expect_identifier (parser_context_t *context_p, /**< context */
parser_raise_error (context_p, PARSER_ERR_IDENTIFIER_EXPECTED);
} /* lexer_expect_identifier */

/**
* Description of "get" literal string.
*/
static const lexer_lit_location_t lexer_get_literal =
{
(const uint8_t *) "get", 3, LEXER_IDENT_LITERAL, false
};

/**
* Description of "set" literal string.
*/
static const lexer_lit_location_t lexer_set_literal =
{
(const uint8_t *) "set", 3, LEXER_IDENT_LITERAL, false
};

/**
* Next token must be an identifier.
*/
Expand Down Expand Up @@ -2266,12 +2250,12 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
if (context_p->source_p < context_p->source_end_p
&& context_p->source_p[0] != LIT_CHAR_COLON)
{
if (lexer_compare_identifier_to_current (context_p, &lexer_get_literal))
if (lexer_compare_raw_identifier_to_current (context_p, "get", 3))
{
context_p->token.type = LEXER_PROPERTY_GETTER;
return;
}
else if (lexer_compare_identifier_to_current (context_p, &lexer_set_literal))
else if (lexer_compare_raw_identifier_to_current (context_p, "set", 3))
{
context_p->token.type = LEXER_PROPERTY_SETTER;
return;
Expand Down Expand Up @@ -2348,11 +2332,11 @@ lexer_scan_identifier (parser_context_t *context_p, /**< context */
if (context_p->source_p < context_p->source_end_p
&& context_p->source_p[0] != LIT_CHAR_COLON)
{
if (lexer_compare_identifier_to_current (context_p, &lexer_get_literal))
if (lexer_compare_raw_identifier_to_current (context_p, "get", 3))
{
context_p->token.type = LEXER_PROPERTY_GETTER;
}
else if (lexer_compare_identifier_to_current (context_p, &lexer_set_literal))
else if (lexer_compare_raw_identifier_to_current (context_p, "set", 3))
{
context_p->token.type = LEXER_PROPERTY_SETTER;
}
Expand All @@ -2376,14 +2360,16 @@ lexer_scan_identifier (parser_context_t *context_p, /**< context */
} /* lexer_scan_identifier */

/**
* Compares the given identifier to that which is the current token
* in the parser context.
* Compares the current identifier in the context to the parameter identifier
*
* Note:
* Escape sequences are allowed.
*
* @return true if the input identifiers are the same
*/
bool
lexer_compare_identifier_to_current (parser_context_t *context_p, /**< context */
const lexer_lit_location_t *right) /**< identifier */
lexer_compare_identifier_to_current (parser_context_t *context_p, /**< context */
const lexer_lit_location_t *right) /**< identifier */
{
lexer_lit_location_t *left = &context_p->token.lit_location;
const uint8_t *left_p;
Expand Down Expand Up @@ -2424,9 +2410,9 @@ lexer_compare_identifier_to_current (parser_context_t *context_p, /**< co

if (*left_p == LIT_CHAR_BACKSLASH && *right_p == LIT_CHAR_BACKSLASH)
{
uint16_t left_chr = lexer_hex_to_character (context_p, left_p, 6);
uint16_t left_chr = lexer_hex_to_character (context_p, left_p + 2, 4);

if (left_chr != lexer_hex_to_character (context_p, right_p, 6))
if (left_chr != lexer_hex_to_character (context_p, right_p + 2, 4))
{
return false;
}
Expand All @@ -2446,7 +2432,7 @@ lexer_compare_identifier_to_current (parser_context_t *context_p, /**< co
right_p = swap_p;
}

utf8_len = lit_char_to_utf8_bytes (utf8_buf, lexer_hex_to_character (context_p, left_p, 6));
utf8_len = lit_char_to_utf8_bytes (utf8_buf, lexer_hex_to_character (context_p, left_p + 2, 4));
JERRY_ASSERT (utf8_len > 0);
count -= utf8_len;
offset = 0;
Expand All @@ -2468,6 +2454,29 @@ lexer_compare_identifier_to_current (parser_context_t *context_p, /**< co
return true;
} /* lexer_compare_identifier_to_current */

/**
* Compares the current identifier in the context to the parameter identifier
*
* Note:
* Escape sequences are not allowed.
*
* @return true if the input identifiers are the same
*/
bool
lexer_compare_raw_identifier_to_current (parser_context_t *context_p, /**< context */
const char *right_ident_p, /**< identifier */
size_t right_ident_length) /**< identifier length */
{
lexer_lit_location_t *left = &context_p->token.lit_location;

if (left->length != right_ident_length || left->has_escape)
{
return 0;
}

return memcmp (left->char_p, right_ident_p, right_ident_length) == 0;
} /* lexer_compare_raw_identifier_to_current */

/**
* @}
* @}
Expand Down
2 changes: 2 additions & 0 deletions jerry-core/parser/js/js-parser-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ void lexer_convert_push_number_to_push_literal (parser_context_t *context_p);
uint16_t lexer_construct_function_object (parser_context_t *context_p, uint32_t extra_status_flags);
void lexer_construct_regexp_object (parser_context_t *context_p, bool parse_only);
bool lexer_compare_identifier_to_current (parser_context_t *context_p, const lexer_lit_location_t *right);
bool lexer_compare_raw_identifier_to_current (parser_context_t *context_p, const char *right_ident_p,
size_t right_ident_length);

/**
* @}
Expand Down
27 changes: 27 additions & 0 deletions tests/jerry/regression-test-issue-2409.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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.

// Must be a success.
eval('g\\u0065t: break get')

try {
// Must be a fail.
eval('({ g\\u0065t a() {} })')
assert(false);
} catch (e) {
assert(e instanceof SyntaxError);
}

// Must be a success.
eval('({ g\\u0065t: 5 })')