From ea859e1cd042578ea17994b56ac15be216eefe4a Mon Sep 17 00:00:00 2001 From: Antonio Borneo Date: Sun, 14 Jul 2024 11:28:49 +0200 Subject: [PATCH] helper: command: drop radix parameter from command_parse_str_to_buf() Commit 53b94fad58ab ("binarybuffer: Fix str_to_buf() parsing function") introduces the helper command_parse_str_to_buf() to parse as number a string on TCL command-line. The parameter 'radix' can specify the base (decimal, octal, hexadecimal, or auto-detected). TCL is supposed to use decimal numbers by default, while octal and hexadecimal numbers must be prefixed respectively with '0' and '0x' (or '0X'). This would require the helper to always run auto-detection of the base, thus always set the 'radix' parameter to zero. This makes the parameter useless. Keeping the 'radix' parameter can open the door to future abuse of TCL syntax, E.g. a command can require an octal value without the mandatory TCL '0' prefix; the octal value cannot be the result of TCL expression. To prevent any future abuse of the 'radix' parameter, drop it. Change-Id: I88855bd83b4e08e8fdcf86a2fa5ef3269dd4ad57 Signed-off-by: Antonio Borneo Reviewed-on: https://review.openocd.org/c/openocd/+/8393 Tested-by: jenkins Reviewed-by: Jan Matyas --- src/helper/command.c | 25 +++---------------------- src/helper/command.h | 8 +++----- src/jtag/tcl.c | 2 +- src/target/target.c | 5 ++--- 4 files changed, 9 insertions(+), 31 deletions(-) diff --git a/src/helper/command.c b/src/helper/command.c index 15a9b4a08..b5dd927f2 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -1360,37 +1360,18 @@ int command_parse_bool_arg(const char *in, bool *out) return ERROR_COMMAND_SYNTAX_ERROR; } -static const char *radix_to_str(unsigned int radix) -{ - switch (radix) { - case 16: return "hexadecimal"; - case 10: return "decadic"; - case 8: return "octal"; - } - assert(false); - return ""; -} - -COMMAND_HELPER(command_parse_str_to_buf, const char *str, void *buf, unsigned int buf_len, - unsigned int radix) +COMMAND_HELPER(command_parse_str_to_buf, const char *str, void *buf, unsigned int buf_len) { assert(str); assert(buf); - int ret = str_to_buf(str, buf, buf_len, radix, NULL); + int ret = str_to_buf(str, buf, buf_len, 0, NULL); if (ret == ERROR_OK) return ret; /* Provide a clear error message to the user */ if (ret == ERROR_INVALID_NUMBER) { - if (radix == 0) { - /* Any radix is accepted, so don't include it in the error message. */ - command_print(CMD, "'%s' is not a valid number", str); - } else { - /* Specific radix is required - tell the user what it is. */ - command_print(CMD, "'%s' is not a valid number (requiring %s number)", - str, radix_to_str(radix)); - } + command_print(CMD, "'%s' is not a valid number", str); } else if (ret == ERROR_NUMBER_EXCEEDS_BUFFER) { command_print(CMD, "Number %s exceeds %u bits", str, buf_len); } else { diff --git a/src/helper/command.h b/src/helper/command.h index 7a044e619..b224bd022 100644 --- a/src/helper/command.h +++ b/src/helper/command.h @@ -519,14 +519,12 @@ COMMAND_HELPER(handle_command_parse_bool, bool *out, const char *label); /** * Parse a number (base 10, base 16 or base 8) and store the result - * into a bit buffer. + * into a bit buffer. Use the prefixes '0' and '0x' for base 8 and 16, + * otherwise defaults to base 10. * * In case of parsing error, a user-readable error message is produced. - * - * If radix = 0 is given, the function guesses the radix by looking at the number prefix. */ -COMMAND_HELPER(command_parse_str_to_buf, const char *str, void *buf, unsigned int buf_len, - unsigned int radix); +COMMAND_HELPER(command_parse_str_to_buf, const char *str, void *buf, unsigned int buf_len); /** parses an on/off command argument */ #define COMMAND_PARSE_ON_OFF(in, out) \ diff --git a/src/jtag/tcl.c b/src/jtag/tcl.c index 10a7dd3f8..624b4e4c2 100644 --- a/src/jtag/tcl.c +++ b/src/jtag/tcl.c @@ -89,7 +89,7 @@ static COMMAND_HELPER(handle_jtag_command_drscan_fields, struct scan_field *fiel } fields[field_count].out_value = t; - int ret = CALL_COMMAND_HANDLER(command_parse_str_to_buf, CMD_ARGV[i + 1], t, bits, 0); + int ret = CALL_COMMAND_HANDLER(command_parse_str_to_buf, CMD_ARGV[i + 1], t, bits); if (ret != ERROR_OK) return ret; fields[field_count].in_value = t; diff --git a/src/target/target.c b/src/target/target.c index b6159c72b..09396d878 100644 --- a/src/target/target.c +++ b/src/target/target.c @@ -3133,7 +3133,7 @@ COMMAND_HANDLER(handle_reg_command) return ERROR_FAIL; } - int retval = CALL_COMMAND_HANDLER(command_parse_str_to_buf, CMD_ARGV[1], buf, reg->size, 0); + int retval = CALL_COMMAND_HANDLER(command_parse_str_to_buf, CMD_ARGV[1], buf, reg->size); if (retval != ERROR_OK) { free(buf); return retval; @@ -4835,8 +4835,7 @@ COMMAND_HANDLER(handle_set_reg_command) return ERROR_FAIL; } - int retval = CALL_COMMAND_HANDLER(command_parse_str_to_buf, - reg_value, buf, reg->size, 0); + int retval = CALL_COMMAND_HANDLER(command_parse_str_to_buf, reg_value, buf, reg->size); if (retval != ERROR_OK) { free(buf); return retval;