From 027fc4373663cc3916abc2979225018f55d9fa23 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Sun, 13 Jan 2019 18:56:19 -0600 Subject: [PATCH] Fix result after explicit `return` in a `while` block Closes #5513. --- src/parse_execution.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/parse_execution.cpp b/src/parse_execution.cpp index f1821176c..116804ffc 100644 --- a/src/parse_execution.cpp +++ b/src/parse_execution.cpp @@ -575,8 +575,13 @@ parse_execution_result_t parse_execution_context_t::run_while_statement( } } + // $status after `while` should be 0 if it executed at least once, otherwise the last `$status` + // obtained by executing the condition is preserved. See #4982. if (loop_executed) { - proc_set_last_status(STATUS_CMD_OK); + // Do not override status if exiting due to the presence of an explict `return xxx` (#5513) + if (!associated_block->skip) { + proc_set_last_status(STATUS_CMD_OK); + } } return ret;