Skip to content

Commit f198b39

Browse files
committed
Fix error handling in scanner when in case of OOM
This patch fixes jerryscript-project#3786 and fixes jerryscript-project#3788. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent dd6d148 commit f198b39

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ scanner_info_t *scanner_insert_info_before (parser_context_t *context_p, const u
358358
scanner_info_t *start_info_p, size_t size);
359359
scanner_literal_pool_t *scanner_push_literal_pool (parser_context_t *context_p, scanner_context_t *scanner_context_p,
360360
uint16_t status_flags);
361+
void scanner_release_active_literal_pool (scanner_context_t *scanner_context_p);
361362
void scanner_pop_literal_pool (parser_context_t *context_p, scanner_context_t *scanner_context_p);
362363
#if ENABLED (JERRY_ES2015)
363364
void scanner_construct_global_block (parser_context_t *context_p, scanner_context_t *scanner_context_p);

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

+17
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,23 @@ scanner_literal_is_arguments (lexer_lit_location_t *literal_p) /**< literal */
463463
return lexer_compare_identifier_to_string (literal_p, (const uint8_t *) "arguments", 9);
464464
} /* scanner_literal_is_arguments */
465465

466+
/**
467+
* Release the active literal pool
468+
*/
469+
void
470+
scanner_release_active_literal_pool (scanner_context_t *scanner_context_p) /**< scanner context */
471+
{
472+
while (scanner_context_p->active_literal_pool_p != NULL)
473+
{
474+
scanner_literal_pool_t *literal_pool_p = scanner_context_p->active_literal_pool_p;
475+
476+
scanner_context_p->active_literal_pool_p = literal_pool_p->prev_p;
477+
478+
parser_list_free (&literal_pool_p->literal_pool);
479+
scanner_free (literal_pool_p, sizeof (scanner_literal_pool_t));
480+
}
481+
} /* scanner_release_active_literal_pool */
482+
466483
/**
467484
* Pop the last literal pool from the end.
468485
*/

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

+17-14
Original file line numberDiff line numberDiff line change
@@ -3159,19 +3159,23 @@ scanner_scan_all (parser_context_t *context_p, /**< context */
31593159
}
31603160
PARSER_CATCH
31613161
{
3162-
/* Ignore the errors thrown by the lexer. */
3163-
if (context_p->error != PARSER_ERR_OUT_OF_MEMORY)
3164-
{
3165-
context_p->error = PARSER_ERR_NO_ERROR;
3166-
}
3167-
31683162
#if ENABLED (JERRY_ES2015)
31693163
while (scanner_context.active_binding_list_p != NULL)
31703164
{
31713165
scanner_pop_binding_list (&scanner_context);
31723166
}
31733167
#endif /* ENABLED (JERRY_ES2015) */
31743168

3169+
if (JERRY_UNLIKELY (context_p->error == PARSER_ERR_OUT_OF_MEMORY))
3170+
{
3171+
scanner_release_active_literal_pool (&scanner_context);
3172+
parser_stack_free (context_p);
3173+
return;
3174+
}
3175+
3176+
/* Ignore the errors thrown by the lexer. */
3177+
context_p->error = PARSER_ERR_NO_ERROR;
3178+
31753179
/* The following code may allocate memory, so it is enclosed in a try/catch. */
31763180
PARSER_TRY (context_p->try_buffer)
31773181
{
@@ -3193,17 +3197,16 @@ scanner_scan_all (parser_context_t *context_p, /**< context */
31933197
}
31943198
PARSER_CATCH
31953199
{
3196-
JERRY_ASSERT (context_p->error == PARSER_ERR_NO_ERROR);
3200+
JERRY_ASSERT (context_p->error == PARSER_ERR_NO_ERROR || context_p->error == PARSER_ERR_OUT_OF_MEMORY);
31973201

3198-
while (scanner_context.active_literal_pool_p != NULL)
3199-
{
3200-
scanner_literal_pool_t *literal_pool_p = scanner_context.active_literal_pool_p;
3202+
scanner_release_active_literal_pool (&scanner_context);
32013203

3202-
scanner_context.active_literal_pool_p = literal_pool_p->prev_p;
3203-
3204-
parser_list_free (&literal_pool_p->literal_pool);
3205-
scanner_free (literal_pool_p, sizeof (scanner_literal_pool_t));
3204+
if (JERRY_UNLIKELY (context_p->error == PARSER_ERR_OUT_OF_MEMORY))
3205+
{
3206+
parser_stack_free (context_p);
3207+
return;
32063208
}
3209+
32073210
}
32083211
PARSER_TRY_END
32093212

0 commit comments

Comments
 (0)