Skip to content

Commit

Permalink
Fix memory leak when using continue or break statement whit syntaxError
Browse files Browse the repository at this point in the history
This patch fixes #5062.

JerryScript-DCO-1.0-Signed-off-by: Gergo Csizi gergocs@inf.u-szeged.hu
  • Loading branch information
gergocs committed Dec 9, 2024
1 parent b3fa5e0 commit 11547fa
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
1 change: 1 addition & 0 deletions jerry-core/parser/js/js-parser-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ typedef struct
ecma_value_t tagged_template_literal_cp; /**< compessed pointer to the tagged template literal collection */
parser_private_context_t *private_context_p; /**< private context */
uint8_t stack_top_uint8; /**< top byte stored on the stack */
parser_list_t branch_list; /**< list of branches */

#ifndef JERRY_NDEBUG
/* Variables for debugging / logging. */
Expand Down
2 changes: 1 addition & 1 deletion jerry-core/parser/js/js-parser-statm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2053,7 +2053,7 @@ parser_parse_case_statement (parser_context_t *context_p) /**< context */
parser_stack_iterator_write (&iterator, &switch_statement, sizeof (parser_switch_statement_t));

parser_set_branch_to_current_position (context_p, &branch_p->branch);
parser_free (branch_p, sizeof (parser_branch_node_t));

} /* parser_parse_case_statement */

/**
Expand Down
3 changes: 1 addition & 2 deletions jerry-core/parser/js/js-parser-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ parser_emit_cbc_forward_branch_item (parser_context_t *context_p, /**< context *
* the branch is constructed locally, and copied later. */
parser_emit_cbc_forward_branch (context_p, opcode, &branch);

new_item = (parser_branch_node_t *) parser_malloc (context_p, sizeof (parser_branch_node_t));
new_item = (parser_branch_node_t *) parser_list_append (context_p, &context_p->branch_list);
new_item->branch = branch;
new_item->next_p = next_p;
return new_item;
Expand Down Expand Up @@ -730,7 +730,6 @@ parser_set_breaks_to_current_position (parser_context_t *context_p, /**< context
{
parser_set_branch_to_current_position (context_p, &current_p->branch);
}
parser_free (current_p, sizeof (parser_branch_node_t));
current_p = next_p;
}
} /* parser_set_breaks_to_current_position */
Expand Down
9 changes: 5 additions & 4 deletions jerry-core/parser/js/js-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2140,6 +2140,9 @@ parser_parse_source (void *source_p, /**< source code */
context.scope_stack_global_end = 0;
context.tagged_template_literal_cp = JMEM_CP_NULL;
context.private_context_p = NULL;
parser_list_init (&context.branch_list,
sizeof (parser_branch_node_t),
(uint32_t) ((128 - sizeof (void *)) / sizeof (parser_branch_node_t)));

#ifndef JERRY_NDEBUG
context.context_stack_depth = 0;
Expand Down Expand Up @@ -2293,6 +2296,7 @@ parser_parse_source (void *source_p, /**< source code */
JERRY_ASSERT (!(context.status_flags & PARSER_HAS_LATE_LIT_INIT));

compiled_code_p = parser_post_processing (&context);
parser_list_free (&context.branch_list);
parser_list_free (&context.literal_pool);

/* When parsing is successful, only the dummy value can be remained on the stack. */
Expand Down Expand Up @@ -2366,10 +2370,6 @@ parser_parse_source (void *source_p, /**< source code */
}
PARSER_CATCH
{
if (context.last_statement.current_p != NULL)
{
parser_free_jumps (context.last_statement);
}

parser_free_allocated_buffer (&context);

Expand All @@ -2383,6 +2383,7 @@ parser_parse_source (void *source_p, /**< source code */
#endif /* JERRY_MODULE_SYSTEM */

compiled_code_p = NULL;
parser_list_free (&context.branch_list);
parser_free_literals (&context.literal_pool);
parser_cbc_stream_free (&context.byte_code);

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

( async ( ) => { for await ( const b of n ) { continue ;
11 changes: 6 additions & 5 deletions tools/run-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def skip_if(condition, desc):
OPTIONS_DOCTESTS = ['--doctests=on', '--jerry-cmdline=off', '--error-messages=on',
'--snapshot-save=on', '--snapshot-exec=on', '--vm-exec-stop=on']
OPTIONS_PROMISE_CALLBACK = ['--promise-callback=on']
OPTIONS_HEAP_SIZE = ['--mem-heap=1024']

# Test options for unittests
JERRY_UNITTESTS_OPTIONS = [
Expand All @@ -67,15 +68,15 @@ def skip_if(condition, desc):
# Test options for jerry-tests
JERRY_TESTS_OPTIONS = [
Options('jerry_tests',
OPTIONS_COMMON + OPTIONS_STACK_LIMIT + OPTIONS_GC_MARK_LIMIT + OPTIONS_MEM_STRESS),
OPTIONS_COMMON + OPTIONS_STACK_LIMIT + OPTIONS_HEAP_SIZE + OPTIONS_GC_MARK_LIMIT + OPTIONS_MEM_STRESS),
Options('jerry_tests-snapshot',
OPTIONS_COMMON + OPTIONS_SNAPSHOT + OPTIONS_STACK_LIMIT + OPTIONS_GC_MARK_LIMIT,
OPTIONS_COMMON + OPTIONS_SNAPSHOT + OPTIONS_HEAP_SIZE + OPTIONS_STACK_LIMIT + OPTIONS_GC_MARK_LIMIT,
['--snapshot']),
Options('jerry_tests-cpointer_32bit',
OPTIONS_COMMON + OPTIONS_STACK_LIMIT + OPTIONS_GC_MARK_LIMIT
+ ['--cpointer-32bit=on', '--mem-heap=1024']),
OPTIONS_COMMON + OPTIONS_STACK_LIMIT + OPTIONS_HEAP_SIZE + OPTIONS_GC_MARK_LIMIT
+ ['--cpointer-32bit=on']),
Options('jerry_tests-external_context',
OPTIONS_COMMON + OPTIONS_STACK_LIMIT + OPTIONS_GC_MARK_LIMIT
OPTIONS_COMMON + OPTIONS_STACK_LIMIT + OPTIONS_HEAP_SIZE + OPTIONS_GC_MARK_LIMIT
+ ['--external-context=on']),
]

Expand Down

0 comments on commit 11547fa

Please sign in to comment.