diff --git a/parse_execution.cpp b/parse_execution.cpp index 72ff611cc..2aa32f82e 100644 --- a/parse_execution.cpp +++ b/parse_execution.cpp @@ -545,6 +545,14 @@ int parse_execution_context_t::run_1_job(const parse_node_t &job_node) /* Populate the job. This may fail for reasons like command_not_found */ bool process_errored = ! this->populate_job_from_job_node(j, job_node); + /* If we errored, we have to clean up the job */ + if (process_errored) + { + assert(parser->current_block()->job == j); + parser->current_block()->job = NULL; + job_free(j); + } + /* Store time it took to 'parse' the command */ if (do_profile) { diff --git a/parser.cpp b/parser.cpp index da6d9f588..3ba3c89be 100644 --- a/parser.cpp +++ b/parser.cpp @@ -2615,6 +2615,8 @@ int parser_t::eval_new_parser(const wcstring &cmd, const io_chain_t &io, enum bl execution_contexts.push_back(ctx); /* Start it up */ + const block_t * const start_current_block = current_block(); + this->push_block(new scope_block_t(block_type)); int result = ctx->eval_top_level_job_list(); /* Clean up the execution context stack */ @@ -2622,6 +2624,21 @@ int parser_t::eval_new_parser(const wcstring &cmd, const io_chain_t &io, enum bl execution_contexts.pop_back(); delete ctx; + /* Clean up the block stack */ + this->pop_block(); + while (start_current_block != current_block()) + { + if (current_block() == NULL) + { + debug(0, + _(L"End of block mismatch. Program terminating.")); + bugreport(); + FATAL_EXIT(); + break; + } + this->pop_block(); + } + /* Reap again */ job_reap(0);