From 999196934ac77a676d1b70968fe0349a6a3f2456 Mon Sep 17 00:00:00 2001 From: Eric Chou Date: Sun, 9 Mar 2025 17:16:46 +0800 Subject: [PATCH] Fix console hang and refactor quit handling - Fixed issue where exceeding the error limit (five attempts) did not exit the console, potentially leading to memory leaks. - Introduced "force_quit" as a helper function to centralize quit handling. - Ensured both "do_quit" and "record_error" call "force_quit", enforcing a consistent function structure. - Reduced code duplication and improved command dispatching logic. Co-authored-by: charliechiou Change-Id: I2f624a16cc4eb3517bf05a65eac105908300bfd9 --- console.c | 60 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/console.c b/console.c index 2f27db6ad..6ba021467 100644 --- a/console.c +++ b/console.c @@ -160,12 +160,43 @@ static char **parse_args(char *line, int *argcp) return argv; } +/* Handles forced console termination for record_error and do_quit */ +static bool force_quit(int argc, char *argv[]) +{ + cmd_element_t *c = cmd_list; + bool ok = true; + while (c) { + cmd_element_t *ele = c; + c = c->next; + free_block(ele, sizeof(cmd_element_t)); + } + + param_element_t *p = param_list; + while (p) { + param_element_t *ele = p; + p = p->next; + free_block(ele, sizeof(param_element_t)); + } + + while (buf_stack) + pop_file(); + + for (int i = 0; i < quit_helper_cnt; i++) { + ok = ok && quit_helpers[i](argc, argv); + } + + quit_flag = true; + return ok; +} + static void record_error() { err_cnt++; if (err_cnt >= err_limit) { - report(1, "Error limit exceeded. Stopping command execution"); - quit_flag = true; + report( + 1, + "Error limit exceeded. Stopping command execution, and quitting"); + force_quit(0, NULL); } } @@ -226,30 +257,7 @@ void set_echo(bool on) /* Built-in commands */ static bool do_quit(int argc, char *argv[]) { - cmd_element_t *c = cmd_list; - bool ok = true; - while (c) { - cmd_element_t *ele = c; - c = c->next; - free_block(ele, sizeof(cmd_element_t)); - } - - param_element_t *p = param_list; - while (p) { - param_element_t *ele = p; - p = p->next; - free_block(ele, sizeof(param_element_t)); - } - - while (buf_stack) - pop_file(); - - for (int i = 0; i < quit_helper_cnt; i++) { - ok = ok && quit_helpers[i](argc, argv); - } - - quit_flag = true; - return ok; + return force_quit(argc, argv); } static bool do_help(int argc, char *argv[])