From: openocd-gerrit <ope...@us...> - 2024-08-25 12:36:25
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "Main OpenOCD repository". The branch, master has been updated via 8a3efbf21fda2ac673c401f8b9ec82d300c2af29 (commit) via ea859e1cd042578ea17994b56ac15be216eefe4a (commit) from a414ffaf65171bf2d1ed289397befca3cbf8b967 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit 8a3efbf21fda2ac673c401f8b9ec82d300c2af29 Author: Antonio Borneo <bor...@gm...> Date: Sun Jul 14 12:09:15 2024 +0200 binarybuffer: simplify the prototype of str_to_buf() With 'radix' always zero and '_detected_radix' always NULL, drop the two parameters and simplify str_to_buf(). While there: - drop some redundant assert(), - drop the re-check for the base prefix, - simplify str_strip_number_prefix_if_present() and rename it, as the prefix MUST be present, - fix a minor typo, - update the doxygen description of str_to_buf(). Change-Id: I1abdc8ec0587b23881953d3094101c04d5bb1c58 Signed-off-by: Antonio Borneo <bor...@gm...> Reviewed-on: https://review.openocd.org/c/openocd/+/8394 Tested-by: jenkins Reviewed-by: Jan Matyas <jan...@co...> diff --git a/src/helper/binarybuffer.c b/src/helper/binarybuffer.c index 3e09143c6..da6e10bab 100644 --- a/src/helper/binarybuffer.c +++ b/src/helper/binarybuffer.c @@ -225,49 +225,37 @@ static bool str_has_octal_prefix(const char *s) */ static unsigned int str_radix_guess(const char *str) { - assert(str); - if (str_has_hex_prefix(str)) return 16; if (str_has_octal_prefix(str)) return 8; - /* Otherwise assume a decadic number. */ + /* Otherwise assume a decimal number. */ return 10; } /** Strip leading "0x" or "0X" from hex numbers or "0" from octal numbers. */ -static void str_strip_number_prefix_if_present(const char **_str, unsigned int radix) +static const char *str_strip_number_prefix(const char *str, unsigned int radix) { - assert(radix == 16 || radix == 10 || radix == 8); - assert(_str); - - const char *str = *_str; - assert(str); - - if (radix == 16 && str_has_hex_prefix(str)) - str += 2; - else if (radix == 8 && str_has_octal_prefix(str)) - str += 1; - - /* No prefix to strip for radix == 10. */ - - *_str = str; + switch (radix) { + case 16: + return str + 2; + case 8: + return str + 1; + case 10: + default: + return str; + } } -int str_to_buf(const char *str, void *_buf, unsigned int buf_len, - unsigned int radix, unsigned int *_detected_radix) +int str_to_buf(const char *str, void *_buf, unsigned int buf_len) { - assert(radix == 0 || radix == 8 || radix == 10 || radix == 16); - - if (radix == 0) - radix = str_radix_guess(str); + assert(str); - if (_detected_radix) - *_detected_radix = radix; + unsigned int radix = str_radix_guess(str); - str_strip_number_prefix_if_present(&str, radix); + str = str_strip_number_prefix(str, radix); const size_t str_len = strlen(str); if (str_len == 0) diff --git a/src/helper/binarybuffer.h b/src/helper/binarybuffer.h index 441374330..6cff86bd9 100644 --- a/src/helper/binarybuffer.h +++ b/src/helper/binarybuffer.h @@ -194,15 +194,14 @@ void *buf_set_buf(const void *src, unsigned src_start, /** * Parse an unsigned number (provided as a zero-terminated string) - * into a bit buffer whose size is buf_len bits. + * into a bit buffer whose size is buf_len bits. The base of the + * number is detected between decimal, hexadecimal and octal. * @param str Input number, zero-terminated string * @param _buf Output buffer, allocated by the caller * @param buf_len Output buffer size in bits - * @param radix Base of the input number - 16, 10, 8 or 0. - * 0 means auto-detect the radix. + * @returns Error on invalid or overflowing number */ -int str_to_buf(const char *str, void *_buf, unsigned int buf_len, - unsigned int radix, unsigned int *_detected_radix); +int str_to_buf(const char *str, void *_buf, unsigned int buf_len); char *buf_to_hex_str(const void *buf, unsigned size); diff --git a/src/helper/command.c b/src/helper/command.c index b5dd927f2..907869325 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -1365,7 +1365,7 @@ COMMAND_HELPER(command_parse_str_to_buf, const char *str, void *buf, unsigned in assert(str); assert(buf); - int ret = str_to_buf(str, buf, buf_len, 0, NULL); + int ret = str_to_buf(str, buf, buf_len); if (ret == ERROR_OK) return ret; commit ea859e1cd042578ea17994b56ac15be216eefe4a Author: Antonio Borneo <bor...@gm...> Date: Sun Jul 14 11:28:49 2024 +0200 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 <bor...@gm...> Reviewed-on: https://review.openocd.org/c/openocd/+/8393 Tested-by: jenkins Reviewed-by: Jan Matyas <jan...@co...> 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; ----------------------------------------------------------------------- Summary of changes: src/helper/binarybuffer.c | 42 +++++++++++++++--------------------------- src/helper/binarybuffer.h | 9 ++++----- src/helper/command.c | 25 +++---------------------- src/helper/command.h | 8 +++----- src/jtag/tcl.c | 2 +- src/target/target.c | 5 ++--- 6 files changed, 28 insertions(+), 63 deletions(-) hooks/post-receive -- Main OpenOCD repository |