Skip to content

Commit 3e54840

Browse files
authored
Invalid regexp literals should throw syntax error in ES11 (#4506)
JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent d97540e commit 3e54840

File tree

11 files changed

+12
-536
lines changed

11 files changed

+12
-536
lines changed

jerry-core/include/jerryscript-snapshot.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extern "C"
3030
/**
3131
* Jerry snapshot format version.
3232
*/
33-
#define JERRY_SNAPSHOT_VERSION (62u)
33+
#define JERRY_SNAPSHOT_VERSION (63u)
3434

3535
/**
3636
* Flags for jerry_generate_snapshot and jerry_generate_function_snapshot.

jerry-core/parser/js/byte-code.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ JERRY_STATIC_ASSERT ((sizeof (cbc_uint16_arguments_t) % sizeof (jmem_cpointer_t)
2727
*/
2828
JERRY_STATIC_ASSERT (CBC_END == 238,
2929
number_of_cbc_opcodes_changed);
30-
JERRY_STATIC_ASSERT (CBC_EXT_END == 149,
30+
JERRY_STATIC_ASSERT (CBC_EXT_END == 148,
3131
number_of_cbc_ext_opcodes_changed);
3232

3333
#if ENABLED (JERRY_PARSER) || ENABLED (JERRY_PARSER_DUMP_BYTE_CODE)

jerry-core/parser/js/byte-code.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,6 @@
610610
VM_OC_LINE) \
611611
CBC_OPCODE (CBC_EXT_THROW_REFERENCE_ERROR, CBC_NO_FLAG, 1, \
612612
VM_OC_THROW_REFERENCE_ERROR) \
613-
CBC_OPCODE (CBC_EXT_THROW_SYNTAX_ERROR, CBC_HAS_LITERAL_ARG, 1, \
614-
VM_OC_THROW_SYNTAX_ERROR | VM_OC_GET_LITERAL) \
615613
CBC_OPCODE (CBC_EXT_THROW_ASSIGN_CONST_ERROR, CBC_NO_FLAG, 0, \
616614
VM_OC_THROW_CONST_ERROR) \
617615
CBC_OPCODE (CBC_EXT_REQUIRE_OBJECT_COERCIBLE, CBC_NO_FLAG, 0, \

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

Lines changed: 3 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -95,42 +95,6 @@ lexer_hex_to_code_point (const uint8_t *source_p, /**< current source position *
9595

9696
#if ENABLED (JERRY_ESNEXT)
9797

98-
/**
99-
* Find a string literal in the literal pool matching with the given buffer's content
100-
*
101-
* @return PARSER_INVALID_LITERAL_INDEX - if the literal is not present in the literal pool
102-
* literal's index in the pool - otherwise
103-
*/
104-
static uint16_t
105-
parser_find_string_literal (parser_context_t *context_p, /**< context */
106-
lexer_literal_t **out_literal_p, /**< [out] found literal */
107-
uint8_t *buffer_p, /**< character buffer */
108-
lit_utf8_size_t size) /**< buffer's size */
109-
{
110-
JERRY_ASSERT (out_literal_p != NULL);
111-
JERRY_ASSERT (buffer_p != NULL);
112-
113-
uint16_t literal_index = 0;
114-
lexer_literal_t *literal_p;
115-
parser_list_iterator_t literal_iterator;
116-
parser_list_iterator_init (&context_p->literal_pool, &literal_iterator);
117-
118-
while ((literal_p = (lexer_literal_t *) parser_list_iterator_next (&literal_iterator)) != NULL)
119-
{
120-
if (literal_p->type == LEXER_STRING_LITERAL
121-
&& literal_p->prop.length == size
122-
&& memcmp (literal_p->u.char_p, buffer_p, size) == 0)
123-
{
124-
*out_literal_p = literal_p;
125-
return literal_index;
126-
}
127-
128-
literal_index++;
129-
}
130-
131-
return PARSER_INVALID_LITERAL_INDEX;
132-
} /* parser_find_string_literal */
133-
13498
/**
13599
* Parse hexadecimal character sequence enclosed in braces
136100
*
@@ -3104,56 +3068,14 @@ lexer_construct_regexp_object (parser_context_t *context_p, /**< context */
31043068
re_compiled_code_t *re_bytecode_p = re_compile_bytecode (pattern_str_p, current_flags);
31053069
ecma_deref_ecma_string (pattern_str_p);
31063070

3107-
lexer_literal_t *literal_p = NULL;
3108-
uint8_t literal_type = LEXER_REGEXP_LITERAL;
3109-
31103071
if (JERRY_UNLIKELY (re_bytecode_p == NULL))
31113072
{
3112-
#if ENABLED (JERRY_ESNEXT)
3113-
ecma_value_t error = jcontext_take_exception ();
3114-
ecma_property_t *prop_p = ecma_find_named_property (ecma_get_object_from_value (error),
3115-
ecma_get_magic_string (LIT_MAGIC_STRING_MESSAGE));
3116-
const char default_msg[] = "Invalid regular expression";
3117-
lit_utf8_byte_t *buffer_p = (lit_utf8_byte_t *) default_msg;
3118-
lit_utf8_size_t size = sizeof (buffer_p) - 1;
3119-
3120-
if (prop_p != NULL)
3121-
{
3122-
ecma_string_t *message_p = ecma_get_string_from_value (ECMA_PROPERTY_VALUE_PTR (prop_p)->value);
3123-
JERRY_ASSERT (!ECMA_IS_DIRECT_STRING (message_p));
3124-
JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (message_p) == ECMA_STRING_CONTAINER_HEAP_ASCII_STRING);
3125-
buffer_p = ECMA_ASCII_STRING_GET_BUFFER (message_p);
3126-
size = ECMA_ASCII_STRING_GET_SIZE (message_p);
3127-
}
3128-
3129-
uint16_t literal_index = parser_find_string_literal (context_p, &literal_p, buffer_p, size);
3130-
3131-
if (literal_index != PARSER_INVALID_LITERAL_INDEX)
3132-
{
3133-
ecma_free_value (error);
3134-
context_p->lit_object.literal_p = literal_p;
3135-
context_p->lit_object.index = literal_index;
3136-
return;
3137-
}
3138-
3139-
literal_p = (lexer_literal_t *) parser_list_append (context_p, &context_p->literal_pool);
3140-
literal_p->u.char_p = (uint8_t *) jmem_heap_alloc_block (size);
3141-
memcpy ((uint8_t *) literal_p->u.char_p, buffer_p, size);
3142-
literal_type = LEXER_STRING_LITERAL;
3143-
length = size;
3144-
3145-
ecma_free_value (error);
3146-
#else /* !ENABLED (JERRY_ESNEXT) */
31473073
parser_raise_error (context_p, PARSER_ERR_INVALID_REGEXP);
3148-
#endif /* ENABLED (JERRY_ESNEXT) */
3149-
}
3150-
else
3151-
{
3152-
literal_p = (lexer_literal_t *) parser_list_append (context_p, &context_p->literal_pool);
3153-
literal_p->u.bytecode_p = (ecma_compiled_code_t *) re_bytecode_p;
31543074
}
31553075

3156-
literal_p->type = literal_type;
3076+
lexer_literal_t *literal_p = (lexer_literal_t *) parser_list_append (context_p, &context_p->literal_pool);
3077+
literal_p->u.bytecode_p = (ecma_compiled_code_t *) re_bytecode_p;
3078+
literal_p->type = LEXER_REGEXP_LITERAL;
31573079
literal_p->prop.length = (prop_length_t) length;
31583080
literal_p->status_flags = 0;
31593081

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,15 +2177,6 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */
21772177
case LEXER_ASSIGN_DIVIDE:
21782178
{
21792179
lexer_construct_regexp_object (context_p, false);
2180-
2181-
#if ENABLED (JERRY_ESNEXT)
2182-
if (JERRY_UNLIKELY (context_p->lit_object.literal_p->type == LEXER_STRING_LITERAL))
2183-
{
2184-
parser_emit_cbc_ext_literal (context_p, CBC_EXT_THROW_SYNTAX_ERROR, context_p->lit_object.index);
2185-
break;
2186-
}
2187-
#endif /* ENABLED (JERRY_ESNEXT) */
2188-
21892180
uint16_t literal_index = (uint16_t) (context_p->literal_count - 1);
21902181

21912182
if (context_p->last_cbc_opcode == CBC_PUSH_LITERAL)

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,7 +2832,6 @@ parser_parse_script (const uint8_t *arg_list_p, /**< function argument list */
28322832
#if ENABLED (JERRY_ERROR_MESSAGES)
28332833
ecma_string_t *err_str_p;
28342834

2835-
#if !ENABLED (JERRY_ESNEXT)
28362835
if (parser_error.error == PARSER_ERR_INVALID_REGEXP)
28372836
{
28382837
ecma_value_t error = jcontext_take_exception ();
@@ -2844,7 +2843,6 @@ parser_parse_script (const uint8_t *arg_list_p, /**< function argument list */
28442843
ecma_ref_ecma_string (err_str_p);
28452844
}
28462845
else
2847-
#endif /* !ENABLED (JERRY_ESNEXT) */
28482846
{
28492847
const lit_utf8_byte_t *err_bytes_p = (const lit_utf8_byte_t *) parser_error_to_string (parser_error.error);
28502848
lit_utf8_size_t err_bytes_size = lit_zt_utf8_string_size (err_bytes_p);
@@ -2865,12 +2863,12 @@ parser_parse_script (const uint8_t *arg_list_p, /**< function argument list */
28652863
ecma_free_value (line_str_val);
28662864
ecma_deref_ecma_string (err_str_p);
28672865
#else /* !ENABLED (JERRY_ERROR_MESSAGES) */
2868-
#if !ENABLED (JERRY_ESNEXT)
2869-
if (parser_error.error != PARSER_ERR_INVALID_REGEXP)
2870-
#endif /* !ENABLED (JERRY_ESNEXT) */
2866+
if (parser_error.error == PARSER_ERR_INVALID_REGEXP)
28712867
{
2872-
ecma_raise_syntax_error ("");
2868+
jcontext_release_exception ();
28732869
}
2870+
2871+
ecma_raise_syntax_error ("");
28742872
#endif /* ENABLED (JERRY_ERROR_MESSAGES) */
28752873

28762874
return NULL;

jerry-core/vm/vm.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,14 +1740,6 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
17401740
result = ecma_raise_type_error (ECMA_ERR_MSG ("Constant bindings cannot be reassigned."));
17411741
goto error;
17421742
}
1743-
case VM_OC_THROW_SYNTAX_ERROR:
1744-
{
1745-
ecma_string_t *msg_p = ecma_get_string_from_value (left_value);
1746-
ecma_object_t *error_obj_p = ecma_new_standard_error (ECMA_ERROR_SYNTAX, msg_p);
1747-
jcontext_raise_exception (ecma_make_object_value (error_obj_p));
1748-
result = ECMA_VALUE_ERROR;
1749-
goto error;
1750-
}
17511743
case VM_OC_COPY_TO_GLOBAL:
17521744
{
17531745
uint32_t literal_index;

jerry-core/vm/vm.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ typedef enum
243243
VM_OC_ASSIGN_LET_CONST, /**< assign values to let/const declarations */
244244
VM_OC_INIT_BINDING, /**< create and intialize a binding */
245245
VM_OC_THROW_CONST_ERROR, /**< throw invalid assignment to const variable error */
246-
VM_OC_THROW_SYNTAX_ERROR, /**< throw syntax error */
247246
VM_OC_COPY_TO_GLOBAL, /**< copy value to global lex env */
248247
VM_OC_COPY_FROM_ARG, /**< copy value from arg lex env */
249248
VM_OC_CLONE_CONTEXT, /**< clone lexical environment with let/const declarations */
@@ -331,7 +330,6 @@ typedef enum
331330
VM_OC_ASSIGN_LET_CONST = VM_OC_NONE, /**< assign values to let/const declarations */
332331
VM_OC_INIT_BINDING = VM_OC_NONE, /**< create and intialize a binding */
333332
VM_OC_THROW_CONST_ERROR = VM_OC_NONE, /**< throw invalid assignment to const variable error */
334-
VM_OC_THROW_SYNTAX_ERROR = VM_OC_NONE, /**< throw syntax error */
335333
VM_OC_COPY_TO_GLOBAL = VM_OC_NONE, /**< copy value to global lex env */
336334
VM_OC_COPY_FROM_ARG = VM_OC_NONE, /**< copy value from arg lex env */
337335
VM_OC_CLONE_CONTEXT = VM_OC_NONE, /**< clone lexical environment with let/const declarations */

tests/jerry/es.next/regression-test-issue-2058.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
try {
16-
/?:/
16+
eval('/?:/');
1717
assert(false);
1818
} catch (e) {
1919
assert(e instanceof SyntaxError);

tests/jerry/es.next/regression-test-issue-4408.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
// limitations under the License.
1414

1515
try {
16-
/(?<=^abc)def/;
16+
eval('/(?<=^abc)def/');
1717
} catch(e) {
1818
assert(e instanceof SyntaxError);
1919
}
2020

2121
try {
22-
/(?a)/;
22+
eval('/(?a)/;')
2323
} catch(e) {
2424
assert(e instanceof SyntaxError);
2525
}

0 commit comments

Comments
 (0)