helper: command: drop radix parameter from command_parse_str_to_buf()
Commit 53b94fad58 ("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 <borneo.antonio@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/8393
Tested-by: jenkins
Reviewed-by: Jan Matyas <jan.matyas@codasip.com>
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user