From 8c983712b0b3363b2aa2970635a24f13eaac1c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Mon, 22 Jan 2018 21:41:52 +0100 Subject: [PATCH] menu: make copy of line to process since strtok can modify original string --- lib/menu.c | 7 +++++-- lib/menu.h | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/menu.c b/lib/menu.c index 95b0345..1320519 100644 --- a/lib/menu.c +++ b/lib/menu.c @@ -27,7 +27,7 @@ #include "menu.h" // definitions for the menu #include "print.h" // print utilities -bool menu_handle_command(char* line, const struct menu_command_t* command_list, size_t command_list_length) +bool menu_handle_command(const char* line, const struct menu_command_t* command_list, size_t command_list_length) { // ensure line is not empty if (!line || 0==strlen(line)) { @@ -39,8 +39,10 @@ bool menu_handle_command(char* line, const struct menu_command_t* command_list, } // get command + char* dup = malloc(strlen(line)+1); // buffer to copy the line + strncpy(dup, line, strlen(line)+1); // make a copy of the line since strtok can modify it const char* delimiter = " "; // words are separated by spaces - char* word = strtok(line, delimiter); // get first word + char* word = strtok(dup, delimiter); // get first word if (!word) { return false; } @@ -90,6 +92,7 @@ bool menu_handle_command(char* line, const struct menu_command_t* command_list, } } + free(dup); // free line copy return command_found; } diff --git a/lib/menu.h b/lib/menu.h index 22a67a3..31b0190 100644 --- a/lib/menu.h +++ b/lib/menu.h @@ -43,7 +43,7 @@ struct menu_command_t { * @prarm[in] command_list_length number of available commands * @return if an command corresponding to the line has been found and called */ -bool menu_handle_command(char* line, const struct menu_command_t* command_list, size_t command_list_length); +bool menu_handle_command(const char* line, const struct menu_command_t* command_list, size_t command_list_length); /** print the commands from the command list * @param[in] command_list list of available commands * @prarm[in] command_list_length number of available commands