diff options
author | Travis Geiselbrecht <geist@foobox.com> | 2013-07-31 11:34:00 -0700 |
---|---|---|
committer | Travis Geiselbrecht <geist@foobox.com> | 2013-07-31 12:12:01 -0700 |
commit | 4979cce3d56bbb008234e6c6718e07b3f815df15 (patch) | |
tree | 47aa350e997a1031184aa6fde0e55ea2068ce9c2 | |
parent | 1fc901fd8cc2324c71937b3cb540aee9fb805b38 (diff) | |
download | lk-4979cce3d56bbb008234e6c6718e07b3f815df15.tar.gz |
[lib][console] Reduce stack size requirement for console apps
- Move args array (256 byte) off stack
- check memory allocation results
- make max number of args #defined
- also free debug buffer
-rw-r--r-- | lib/console/console.c | 47 |
1 files changed, 41 insertions, 6 deletions
diff --git a/lib/console/console.c b/lib/console/console.c index c8e575f1..e783efaf 100644 --- a/lib/console/console.c +++ b/lib/console/console.c @@ -22,6 +22,7 @@ */ #include <debug.h> #include <assert.h> +#include <err.h> #include <string.h> #include <ctype.h> #include <stdio.h> @@ -39,6 +40,8 @@ #define LINE_LEN 128 +#define MAX_NUM_ARGS 16 + #define HISTORY_LEN 16 #define LOCAL_TRACE 0 @@ -525,16 +528,28 @@ static void convert_args(int argc, cmd_args *argv) } } -static void command_loop(int (*get_line)(const char **, void *), void *get_line_cookie, bool showprompt, bool locked) + +static status_t command_loop(int (*get_line)(const char **, void *), void *get_line_cookie, bool showprompt, bool locked) { bool exit; - cmd_args args[16]; +#if WITH_LIB_ENV + bool report_result; +#endif + cmd_args *args = NULL; const char *buffer; const char *continuebuffer; - char *outbuf; + char *outbuf = NULL; + + args = (cmd_args *) malloc (MAX_NUM_ARGS * sizeof(cmd_args)); + if (unlikely(args == NULL)) { + goto no_mem_error; + } const size_t outbuflen = 1024; outbuf = malloc(outbuflen); + if (unlikely(outbuf == NULL)) { + goto no_mem_error; + } exit = false; continuebuffer = NULL; @@ -556,7 +571,8 @@ static void command_loop(int (*get_line)(const char **, void *), void *get_line_ // dprintf("line = '%s'\n", buffer); /* tokenize the line */ - int argc = tokenize_command(buffer, &continuebuffer, outbuf, outbuflen, args, 16); + int argc = tokenize_command(buffer, &continuebuffer, outbuf, outbuflen, + args, MAX_NUM_ARGS); if (argc < 0) { if (showprompt) printf("syntax error\n"); @@ -612,6 +628,18 @@ static void command_loop(int (*get_line)(const char **, void *), void *get_line_ } free(outbuf); + free(args); + return NO_ERROR; + +no_mem_error: + if (outbuf) + free(outbuf); + + if (args) + free(args); + + dprintf(INFO, "%s: not enough memory\n", __func__); + return ERR_NO_MEMORY; } void console_abort_script(void) @@ -625,8 +653,13 @@ void console_start(void) dprintf(INFO, "entering main console loop\n"); - for (;;) - command_loop(&read_debug_line, NULL, true, false); + + while (command_loop(&read_debug_line, NULL, true, false) == NO_ERROR) + ; + + dprintf(INFO, "exiting main console loop\n"); + + free (debug_buffer); } struct line_read_struct { @@ -674,6 +707,8 @@ static int console_run_script_etc(const char *string, bool locked) command_loop(&fetch_next_line, (void *)&lineread, false, locked); + free(lineread.buffer); + return lastresult; } |