From 4d42b5ea24dd7cc26f7776e4c05aec1a8d6942f5 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 13 Oct 2017 07:12:08 +0800 Subject: [PATCH] console: handle empty input to esp_console_run correctly Fixes https://github.com/espressif/esp-idf/issues/1067 Ref. TW15752 --- components/console/commands.c | 4 +++- components/console/esp_console.h | 2 ++ examples/system/console/main/console_example_main.c | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/components/console/commands.c b/components/console/commands.c index dfcbf450c..c52c7061d 100644 --- a/components/console/commands.c +++ b/components/console/commands.c @@ -185,7 +185,9 @@ esp_err_t esp_console_run(const char* cmdline, int* cmd_ret) size_t argc = esp_console_split_argv(s_tmp_line_buf, argv, s_config.max_cmdline_args); - + if (argc == 0) { + return ESP_ERR_INVALID_ARG; + } const cmd_item_t* cmd = find_command_by_name(argv[0]); if (cmd == NULL) { return ESP_ERR_NOT_FOUND; diff --git a/components/console/esp_console.h b/components/console/esp_console.h index 17cf5fe79..bea7eee72 100644 --- a/components/console/esp_console.h +++ b/components/console/esp_console.h @@ -107,6 +107,8 @@ esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd); * @param[out] cmd_ret return code from the command (set if command was run) * @return * - ESP_OK, if command was run + * - ESP_ERR_INVALID_ARG, if the command line is empty, or only contained + * whitespace * - ESP_ERR_NOT_FOUND, if command with given name wasn't registered * - ESP_ERR_INVALID_STATE, if esp_console_init wasn't called */ diff --git a/examples/system/console/main/console_example_main.c b/examples/system/console/main/console_example_main.c index c849d5494..4cd1ac830 100644 --- a/examples/system/console/main/console_example_main.c +++ b/examples/system/console/main/console_example_main.c @@ -153,6 +153,8 @@ void app_main() esp_err_t err = esp_console_run(line, &ret); if (err == ESP_ERR_NOT_FOUND) { printf("Unrecognized command\n"); + } else if (err == ESP_ERR_INVALID_ARG) { + // command was empty } else if (err == ESP_OK && ret != ESP_OK) { printf("Command returned non-zero error code: 0x%x\n", ret); } else if (err != ESP_OK) {