From a932810f9d55256caea8c65a79506e838a17c316 Mon Sep 17 00:00:00 2001 From: Tomas Vanek Date: Thu, 29 Oct 2020 18:32:13 +0100 Subject: helper/command: fix clang static analyzer warning Warning: line 955, column 3 Argument to free() is the address of a global variable, which is not memory allocated by malloc() It is definitely a false alarm. Simplify concatenation of arguments and allocate a string always to silence the warning. Change-Id: I5ac4cc610fc35224df0b16ef4f7102700363249f Signed-off-by: Tomas Vanek Reviewed-on: http://openocd.zylin.com/5904 Reviewed-by: Antonio Borneo Tested-by: jenkins Reviewed-by: Andreas Fritiofson --- src/helper/command.c | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) (limited to 'src/helper/command.c') diff --git a/src/helper/command.c b/src/helper/command.c index 271e7b9..773195e 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -920,39 +920,29 @@ COMMAND_HANDLER(handle_help_command) bool full = strcmp(CMD_NAME, "help") == 0; int retval; struct command *c = CMD_CTX->commands; - char *cmd_match = NULL; - - if (CMD_ARGC == 0) - cmd_match = ""; - else if (CMD_ARGC >= 1) { - unsigned i; - - for (i = 0; i < CMD_ARGC; ++i) { - if (NULL != cmd_match) { - char *prev = cmd_match; - - cmd_match = alloc_printf("%s %s", cmd_match, CMD_ARGV[i]); - free(prev); - if (NULL == cmd_match) { - LOG_ERROR("unable to build search string"); - return -ENOMEM; - } - } else { - cmd_match = alloc_printf("%s", CMD_ARGV[i]); - if (NULL == cmd_match) { - LOG_ERROR("unable to build search string"); - return -ENOMEM; - } - } + char *cmd_match; + + if (CMD_ARGC <= 0) + cmd_match = strdup(""); + + else { + cmd_match = strdup(CMD_ARGV[0]); + + for (unsigned int i = 1; i < CMD_ARGC && cmd_match; ++i) { + char *prev = cmd_match; + cmd_match = alloc_printf("%s %s", prev, CMD_ARGV[i]); + free(prev); } - } else - return ERROR_COMMAND_SYNTAX_ERROR; + } + if (cmd_match == NULL) { + LOG_ERROR("unable to build search string"); + return -ENOMEM; + } retval = CALL_COMMAND_HANDLER(command_help_show_list, c, 0, full, cmd_match); - if (CMD_ARGC >= 1) - free(cmd_match); + free(cmd_match); return retval; } -- cgit v1.1 From a03ac1ba3087ffedb3064e926624eda6cda8f808 Mon Sep 17 00:00:00 2001 From: Tomas Vanek Date: Sun, 8 Nov 2020 13:46:39 +0100 Subject: helper/command: disable logging of registered commands [RFC] Every debug log of OpenOCD contains approximately 130 lines like: Debug: 264 147 command.c:354 register_command_handler(): registering 'flash'... Because only root name of the command is logged, most of lines is not too informative. E.g. registering 'flash' is repeated 14 times. Karl Passon submitted the patch [1] changing the logged cmd name from root to lowest level. It makes the log better. Unfortunately we also have 'reset_config' and 'cortex_m reset_config' and similar which looks equal in the log after [1]. Moreover [1] has not been reviewed for 5 years. So my guess is that nobody uses that crap in debug log. Save more than 10 kbytes in any debug log and make log analyse easier by blocking log command in #if 0 block. If some developer eventually needs to debug cmd registering he can easily enable logging again. [1] http://openocd.zylin.com/2765 Change-Id: Ib7e528aadd692fd0da2e3c005b4c5a484551b728 Signed-off-by: Tomas Vanek Reviewed-on: http://openocd.zylin.com/5928 Tested-by: jenkins Reviewed-by: Antonio Borneo Reviewed-by: Christopher Head --- src/helper/command.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/helper/command.c') diff --git a/src/helper/command.c b/src/helper/command.c index 773195e..0a711e5 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -349,7 +349,9 @@ static int register_command_handler(struct command_context *cmd_ctx, { Jim_Interp *interp = cmd_ctx->interp; +#if 0 LOG_DEBUG("registering '%s'...", c->name); +#endif Jim_CmdProc *func = c->handler ? &script_command : &command_unknown; int retval = Jim_CreateCommand(interp, c->name, func, c, NULL); -- cgit v1.1