Skip to content

Commit e53dfb4

Browse files
committed
Fix memory leak when using continue or break statement with syntaxError
This patch fixes jerryscript-project#5062. JerryScript-DCO-1.0-Signed-off-by: Gergo Csizi [email protected]
1 parent b3fa5e0 commit e53dfb4

File tree

6 files changed

+28
-13
lines changed

6 files changed

+28
-13
lines changed

jerry-core/parser/js/js-parser-internal.h

+1
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ typedef struct
591591
ecma_value_t tagged_template_literal_cp; /**< compessed pointer to the tagged template literal collection */
592592
parser_private_context_t *private_context_p; /**< private context */
593593
uint8_t stack_top_uint8; /**< top byte stored on the stack */
594+
parser_list_t branch_list; /**< list of branches */
594595

595596
#ifndef JERRY_NDEBUG
596597
/* Variables for debugging / logging. */

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

-1
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,6 @@ parser_parse_case_statement (parser_context_t *context_p) /**< context */
20532053
parser_stack_iterator_write (&iterator, &switch_statement, sizeof (parser_switch_statement_t));
20542054

20552055
parser_set_branch_to_current_position (context_p, &branch_p->branch);
2056-
parser_free (branch_p, sizeof (parser_branch_node_t));
20572056
} /* parser_parse_case_statement */
20582057

20592058
/**

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ parser_emit_cbc_forward_branch_item (parser_context_t *context_p, /**< context *
540540
* the branch is constructed locally, and copied later. */
541541
parser_emit_cbc_forward_branch (context_p, opcode, &branch);
542542

543-
new_item = (parser_branch_node_t *) parser_malloc (context_p, sizeof (parser_branch_node_t));
543+
new_item = (parser_branch_node_t *) parser_list_append (context_p, &context_p->branch_list);
544544
new_item->branch = branch;
545545
new_item->next_p = next_p;
546546
return new_item;
@@ -730,7 +730,6 @@ parser_set_breaks_to_current_position (parser_context_t *context_p, /**< context
730730
{
731731
parser_set_branch_to_current_position (context_p, &current_p->branch);
732732
}
733-
parser_free (current_p, sizeof (parser_branch_node_t));
734733
current_p = next_p;
735734
}
736735
} /* parser_set_breaks_to_current_position */

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,9 @@ parser_parse_source (void *source_p, /**< source code */
21402140
context.scope_stack_global_end = 0;
21412141
context.tagged_template_literal_cp = JMEM_CP_NULL;
21422142
context.private_context_p = NULL;
2143+
parser_list_init (&context.branch_list,
2144+
sizeof (parser_branch_node_t),
2145+
(uint32_t) ((128 - sizeof (void *)) / sizeof (parser_branch_node_t)));
21432146

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

22952298
compiled_code_p = parser_post_processing (&context);
2299+
parser_list_free (&context.branch_list);
22962300
parser_list_free (&context.literal_pool);
22972301

22982302
/* When parsing is successful, only the dummy value can be remained on the stack. */
@@ -2366,11 +2370,6 @@ parser_parse_source (void *source_p, /**< source code */
23662370
}
23672371
PARSER_CATCH
23682372
{
2369-
if (context.last_statement.current_p != NULL)
2370-
{
2371-
parser_free_jumps (context.last_statement);
2372-
}
2373-
23742373
parser_free_allocated_buffer (&context);
23752374

23762375
scanner_cleanup (&context);
@@ -2383,6 +2382,7 @@ parser_parse_source (void *source_p, /**< source code */
23832382
#endif /* JERRY_MODULE_SYSTEM */
23842383

23852384
compiled_code_p = NULL;
2385+
parser_list_free (&context.branch_list);
23862386
parser_free_literals (&context.literal_pool);
23872387
parser_cbc_stream_free (&context.byte_code);
23882388

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
( async ( ) => { for await ( const b of n ) { continue ;

tools/run-tests.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def skip_if(condition, desc):
5050
OPTIONS_DOCTESTS = ['--doctests=on', '--jerry-cmdline=off', '--error-messages=on',
5151
'--snapshot-save=on', '--snapshot-exec=on', '--vm-exec-stop=on']
5252
OPTIONS_PROMISE_CALLBACK = ['--promise-callback=on']
53+
OPTIONS_HEAP_SIZE = ['--mem-heap=1024']
5354

5455
# Test options for unittests
5556
JERRY_UNITTESTS_OPTIONS = [
@@ -67,15 +68,15 @@ def skip_if(condition, desc):
6768
# Test options for jerry-tests
6869
JERRY_TESTS_OPTIONS = [
6970
Options('jerry_tests',
70-
OPTIONS_COMMON + OPTIONS_STACK_LIMIT + OPTIONS_GC_MARK_LIMIT + OPTIONS_MEM_STRESS),
71+
OPTIONS_COMMON + OPTIONS_STACK_LIMIT + OPTIONS_HEAP_SIZE + OPTIONS_GC_MARK_LIMIT + OPTIONS_MEM_STRESS),
7172
Options('jerry_tests-snapshot',
72-
OPTIONS_COMMON + OPTIONS_SNAPSHOT + OPTIONS_STACK_LIMIT + OPTIONS_GC_MARK_LIMIT,
73+
OPTIONS_COMMON + OPTIONS_SNAPSHOT + OPTIONS_HEAP_SIZE + OPTIONS_STACK_LIMIT + OPTIONS_GC_MARK_LIMIT,
7374
['--snapshot']),
7475
Options('jerry_tests-cpointer_32bit',
75-
OPTIONS_COMMON + OPTIONS_STACK_LIMIT + OPTIONS_GC_MARK_LIMIT
76-
+ ['--cpointer-32bit=on', '--mem-heap=1024']),
76+
OPTIONS_COMMON + OPTIONS_STACK_LIMIT + OPTIONS_HEAP_SIZE + OPTIONS_GC_MARK_LIMIT
77+
+ ['--cpointer-32bit=on']),
7778
Options('jerry_tests-external_context',
78-
OPTIONS_COMMON + OPTIONS_STACK_LIMIT + OPTIONS_GC_MARK_LIMIT
79+
OPTIONS_COMMON + OPTIONS_STACK_LIMIT + OPTIONS_HEAP_SIZE + OPTIONS_GC_MARK_LIMIT
7980
+ ['--external-context=on']),
8081
]
8182

0 commit comments

Comments
 (0)