Skip to content
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
19 changes: 19 additions & 0 deletions jerry-core/parser/js/js-lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,9 @@ lexer_parse_string (parser_context_t *context_p, /**< context */
else if (*source_p == LEXER_NEWLINE_LS_PS_BYTE_1 && LEXER_NEWLINE_LS_PS_BYTE_23 (source_p))
{
source_p += 3;
#if ENABLED (JERRY_ES2015)
length += 3 * raw_length_inc;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this raw_length_inc, or can the raw length be computed as source_p - string_start_p. If yes, perhaps we could just remove the variable. This could be more future proof approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO that way is not completely valid, since in raw strings there are character sequences like (\r\n) which going to be transformed (into \n) therefore a single subtraction is not enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we could remove raw_length_inc, but we still would need another variable (which also has to be guarded with JERRY_ES2015) to check for \r\n to \n conversions, as rerobika said. Also, source_p points to one character after literal ends when there is $ in string. So length (for raw string) would be source_p - string_start_p - raw_length_dec

@zherczeg I can add it to this commit, there is not so much to change, if you think this would improve performance or code readability :)

#endif /* ENABLED (JERRY_ES2015) */
line++;
column = 1;
continue;
Expand All @@ -982,6 +985,12 @@ lexer_parse_string (parser_context_t *context_p, /**< context */
#if ENABLED (JERRY_ES2015)
if (opts & LEXER_STRING_RAW)
{
if ((*source_p == LIT_CHAR_GRAVE_ACCENT) || (*source_p == LIT_CHAR_BACKSLASH))
{
source_p++;
column++;
length++;
}
continue;
}
#endif /* ENABLED (JERRY_ES2015) */
Expand Down Expand Up @@ -2206,6 +2215,16 @@ lexer_convert_literal_to_chars (parser_context_t *context_p, /**< context */
}
continue;
}
if ((*source_p == LIT_CHAR_BACKSLASH) && is_raw)
{
JERRY_ASSERT (source_p + 1 < context_p->source_end_p);
if ((*(source_p + 1) == LIT_CHAR_GRAVE_ACCENT) || (*(source_p + 1) == LIT_CHAR_BACKSLASH))
{
*destination_p++ = *source_p++;
*destination_p++ = *source_p++;
continue;
}
}
}
#endif /* ENABLED (JERRY_ES2015) */

Expand Down
2 changes: 1 addition & 1 deletion jerry-core/parser/js/js-parser-tagged-template-literal.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ parser_tagged_template_literal_append_strings (parser_context_t *context_p, /**<
{
lexer_lit_location_t *lit_loc_p = &context_p->token.lit_location;

if (lit_loc_p->length == 0)
if (lit_loc_p->length == 0 && !lit_loc_p->has_escape)
{
ecma_builtin_helper_def_prop_by_index (template_obj_p,
prop_idx,
Expand Down
26 changes: 26 additions & 0 deletions tests/jerry/es2015/string-raw-crash-escaping-backslash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* 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.
*/

/* This test must be left unchanged - testing JerryScript crash */

try
{
var s = 'print(String.raw`\\`)\n// `';
eval (s)
asserts(false)
}
catch (error)
{
}
7 changes: 7 additions & 0 deletions tests/jerry/es2015/string-raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,10 @@ var raw = new Proxy({length: 2, 0: '', 1: ''}, { get: function(o, k) { get.push(
var p = new Proxy({raw: raw}, { get: function(o, k) { get.push(k); return o[k]; }});
String.raw(p);
assert(get + '' === "raw,length,0,1");

assert(String.raw`\\` == "\\\\")
assert(String.raw`\`` == "\\`")
assert(String.raw`\
\
` == "\\\n\\\n")
assert(String.raw`\
` == "\\\u2029")