binarybuffer: str_to_buf(): simplify it and fix scan-build error

The function str_to_buf() can be simplified by writing directly
the intermediate results in the output buffer.
Such simplification improves the readability and also makes
scan-build happy, as it does not trigger anymore the warning:
	src/helper/binarybuffer.c:328:8: warning: Use of memory
	allocated with size zero [unix.Malloc]
		if ((b256_buf[(buf_len / 8)] & mask) != 0x0) {

Change-Id: I1cef9a1ec5ff0e5841ba582610f273e89e7a81da
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/8396
Reviewed-by: Jan Matyas <jan.matyas@codasip.com>
Tested-by: jenkins
This commit is contained in:
Antonio Borneo 2024-07-14 14:59:34 +02:00
parent bee5999a44
commit bfd3110e59
1 changed files with 26 additions and 73 deletions

View File

@ -175,19 +175,6 @@ uint32_t flip_u32(uint32_t value, unsigned int num)
return c; return c;
} }
static int ceil_f_to_u32(float x)
{
if (x < 0) /* return zero for negative numbers */
return 0;
uint32_t y = x; /* cut off fraction */
if ((x - y) > 0.0) /* if there was a fractional part, increase by one */
y++;
return y;
}
char *buf_to_hex_str(const void *_buf, unsigned buf_len) char *buf_to_hex_str(const void *_buf, unsigned buf_len)
{ {
unsigned len_bytes = DIV_ROUND_UP(buf_len, 8); unsigned len_bytes = DIV_ROUND_UP(buf_len, 8);
@ -252,7 +239,10 @@ static const char *str_strip_number_prefix(const char *str, unsigned int radix)
int str_to_buf(const char *str, void *_buf, unsigned int buf_bitsize) int str_to_buf(const char *str, void *_buf, unsigned int buf_bitsize)
{ {
assert(str); assert(str);
assert(_buf);
assert(buf_bitsize > 0);
uint8_t *buf = _buf;
unsigned int radix = str_radix_guess(str); unsigned int radix = str_radix_guess(str);
str = str_strip_number_prefix(str, radix); str = str_strip_number_prefix(str, radix);
@ -261,86 +251,49 @@ int str_to_buf(const char *str, void *_buf, unsigned int buf_bitsize)
if (str_len == 0) if (str_len == 0)
return ERROR_INVALID_NUMBER; return ERROR_INVALID_NUMBER;
float factor = 0.0; const size_t buf_len = DIV_ROUND_UP(buf_bitsize, 8);
if (radix == 16) memset(buf, 0, buf_len);
factor = 0.5; /* log(16) / log(256) = 0.5 */
else if (radix == 10)
factor = 0.41524; /* log(10) / log(256) = 0.41524 */
else if (radix == 8)
factor = 0.375; /* log(8) / log(256) = 0.375 */
else
assert(false);
const unsigned int b256_len = ceil_f_to_u32(str_len * factor);
/* Allocate a buffer for digits in base-256 notation */
uint8_t *b256_buf = calloc(b256_len, 1);
if (!b256_buf) {
LOG_ERROR("Unable to allocate memory");
return ERROR_FAIL;
}
/* Go through the zero-terminated buffer /* Go through the zero-terminated buffer
* of input digits (ASCII) */ * of input digits (ASCII) */
for (unsigned int i = 0; str[i]; i++) { for (; *str; str++) {
uint32_t tmp = str[i]; unsigned int tmp;
if ((tmp >= '0') && (tmp <= '9')) { const char c = *str;
tmp = (tmp - '0');
} else if ((tmp >= 'a') && (tmp <= 'f')) { if ((c >= '0') && (c <= '9')) {
tmp = (tmp - 'a' + 10); tmp = c - '0';
} else if ((tmp >= 'A') && (tmp <= 'F')) { } else if ((c >= 'a') && (c <= 'f')) {
tmp = (tmp - 'A' + 10); tmp = c - 'a' + 10;
} else if ((c >= 'A') && (c <= 'F')) {
tmp = c - 'A' + 10;
} else { } else {
/* Characters other than [0-9,a-f,A-F] are invalid */ /* Characters other than [0-9,a-f,A-F] are invalid */
free(b256_buf);
return ERROR_INVALID_NUMBER; return ERROR_INVALID_NUMBER;
} }
if (tmp >= radix) { /* Error on invalid digit for current radix */
/* Encountered a digit that is invalid for the current radix */ if (tmp >= radix)
free(b256_buf);
return ERROR_INVALID_NUMBER; return ERROR_INVALID_NUMBER;
}
/* Add the current digit (tmp) to the intermediate result /* Add the current digit (tmp) to the intermediate result in buf */
* in b256_buf (base-256 digits) */ for (unsigned int j = 0; j < buf_len; j++) {
for (unsigned int j = 0; j < b256_len; j++) { tmp += buf[j] * radix;
tmp += (uint32_t)b256_buf[j] * radix; buf[j] = tmp & 0xFFu;
b256_buf[j] = (uint8_t)(tmp & 0xFFu);
tmp >>= 8; tmp >>= 8;
} }
/* The b256_t buffer is large enough to contain the whole result. */ /* buf should be large enough to contain the whole result. */
assert(tmp == 0); if (tmp != 0)
return ERROR_NUMBER_EXCEEDS_BUFFER;
} }
/* The result must not contain more bits than buf_bitsize. */ /* Check the partial most significant byte */
/* Check the whole bytes: */
for (unsigned int j = DIV_ROUND_UP(buf_bitsize, 8); j < b256_len; j++) {
if (b256_buf[j] != 0x0) {
free(b256_buf);
return ERROR_NUMBER_EXCEEDS_BUFFER;
}
}
/* Check the partial byte: */
if (buf_bitsize % 8) { if (buf_bitsize % 8) {
const uint8_t mask = 0xFFu << (buf_bitsize % 8); const uint8_t mask = 0xFFu << (buf_bitsize % 8);
if ((b256_buf[(buf_bitsize / 8)] & mask) != 0x0) { if ((buf[buf_len - 1] & mask) != 0x0)
free(b256_buf);
return ERROR_NUMBER_EXCEEDS_BUFFER; return ERROR_NUMBER_EXCEEDS_BUFFER;
}
} }
/* Copy the digits to the output buffer */
uint8_t *buf = _buf;
for (unsigned int j = 0; j < DIV_ROUND_UP(buf_bitsize, 8); j++) {
if (j < b256_len)
buf[j] = b256_buf[j];
else
buf[j] = 0;
}
free(b256_buf);
return ERROR_OK; return ERROR_OK;
} }