Skip to content

Commit

Permalink
Fix error handling in scanner when in case of OOM
Browse files Browse the repository at this point in the history
This patch fixes jerryscript-project#3786 and fixes jerryscript-project#3788.

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
  • Loading branch information
rerobika committed May 27, 2020
1 parent 8f76a1f commit 2de1838
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
1 change: 1 addition & 0 deletions jerry-core/parser/js/js-scanner-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ scanner_info_t *scanner_insert_info_before (parser_context_t *context_p, const u
scanner_info_t *start_info_p, size_t size);
scanner_literal_pool_t *scanner_push_literal_pool (parser_context_t *context_p, scanner_context_t *scanner_context_p,
uint16_t status_flags);
void scanner_release_all_literal_pools (scanner_context_t *scanner_context_p);
void scanner_pop_literal_pool (parser_context_t *context_p, scanner_context_t *scanner_context_p);
#if ENABLED (JERRY_ES2015)
void scanner_construct_global_block (parser_context_t *context_p, scanner_context_t *scanner_context_p);
Expand Down
17 changes: 17 additions & 0 deletions jerry-core/parser/js/js-scanner-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,23 @@ scanner_literal_is_arguments (lexer_lit_location_t *literal_p) /**< literal */
return lexer_compare_identifier_to_string (literal_p, (const uint8_t *) "arguments", 9);
} /* scanner_literal_is_arguments */

/**
* Release all literal pools
*/
void
scanner_release_all_literal_pools (scanner_context_t *scanner_context_p) /**< scanner context */
{
while (scanner_context_p->active_literal_pool_p != NULL)
{
scanner_literal_pool_t *literal_pool_p = scanner_context_p->active_literal_pool_p;

scanner_context_p->active_literal_pool_p = literal_pool_p->prev_p;

parser_list_free (&literal_pool_p->literal_pool);
scanner_free (literal_pool_p, sizeof (scanner_literal_pool_t));
}
} /* scanner_release_all_literal_pools */

/**
* Pop the last literal pool from the end.
*/
Expand Down
30 changes: 14 additions & 16 deletions jerry-core/parser/js/js-scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -3159,19 +3159,23 @@ scanner_scan_all (parser_context_t *context_p, /**< context */
}
PARSER_CATCH
{
/* Ignore the errors thrown by the lexer. */
if (context_p->error != PARSER_ERR_OUT_OF_MEMORY)
{
context_p->error = PARSER_ERR_NO_ERROR;
}

#if ENABLED (JERRY_ES2015)
while (scanner_context.active_binding_list_p != NULL)
{
scanner_pop_binding_list (&scanner_context);
}
#endif /* ENABLED (JERRY_ES2015) */

if (JERRY_UNLIKELY (context_p->error == PARSER_ERR_OUT_OF_MEMORY))
{
scanner_release_all_literal_pools (&scanner_context);
parser_stack_free (context_p);
return;
}

/* Ignore the errors thrown by the lexer. */
context_p->error = PARSER_ERR_NO_ERROR;

/* The following code may allocate memory, so it is enclosed in a try/catch. */
PARSER_TRY (context_p->try_buffer)
{
Expand All @@ -3193,17 +3197,11 @@ scanner_scan_all (parser_context_t *context_p, /**< context */
}
PARSER_CATCH
{
JERRY_ASSERT (context_p->error == PARSER_ERR_NO_ERROR);
JERRY_ASSERT (context_p->error == PARSER_ERR_OUT_OF_MEMORY);

while (scanner_context.active_literal_pool_p != NULL)
{
scanner_literal_pool_t *literal_pool_p = scanner_context.active_literal_pool_p;

scanner_context.active_literal_pool_p = literal_pool_p->prev_p;

parser_list_free (&literal_pool_p->literal_pool);
scanner_free (literal_pool_p, sizeof (scanner_literal_pool_t));
}
scanner_release_all_literal_pools (&scanner_context);
parser_stack_free (context_p);
return;
}
PARSER_TRY_END

Expand Down

0 comments on commit 2de1838

Please sign in to comment.