app/testpmd: fix hexadecimal parser with odd length
Current hex string parser assumes input has even characters number.
The parser fails input string with odd length.
The patch parses hex strings with even and odd length.
Parse result of an input with odd length will match result of
even length input, that has `0` as MSB, following by the original
sequence.
For example:
"0x1" results in *dst={0x01, 0x00}, *size=1
"0xabc" results in *dst={0x0a, 0xbc, 0x00}, *size=2
Fixes: 169a9fed1f
("app/testpmd: fix hex string parser support for flow API")
Cc: stable@dpdk.org
Signed-off-by: Gregory Etelson <getelson@nvidia.com>
Reviewed-by: Viacheslav Ovsiienko <viacheslavo@nvidia.com>
This commit is contained in:
parent
3f7b90eb80
commit
ea1da434c0
@ -7702,9 +7702,8 @@ parse_string(struct context *ctx, const struct token *token,
|
|||||||
static int
|
static int
|
||||||
parse_hex_string(const char *src, uint8_t *dst, uint32_t *size)
|
parse_hex_string(const char *src, uint8_t *dst, uint32_t *size)
|
||||||
{
|
{
|
||||||
char *c = NULL;
|
uint32_t left = *size;
|
||||||
uint32_t i, len;
|
const uint8_t *head = dst;
|
||||||
char tmp[3];
|
|
||||||
|
|
||||||
/* Check input parameters */
|
/* Check input parameters */
|
||||||
if ((src == NULL) ||
|
if ((src == NULL) ||
|
||||||
@ -7714,19 +7713,23 @@ parse_hex_string(const char *src, uint8_t *dst, uint32_t *size)
|
|||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
/* Convert chars to bytes */
|
/* Convert chars to bytes */
|
||||||
for (i = 0, len = 0; i < *size; i += 2) {
|
while (left) {
|
||||||
snprintf(tmp, 3, "%s", src + i);
|
char tmp[3], *end = tmp;
|
||||||
dst[len++] = strtoul(tmp, &c, 16);
|
uint32_t read_lim = left & 1 ? 1 : 2;
|
||||||
if (*c != 0) {
|
|
||||||
len--;
|
snprintf(tmp, read_lim + 1, "%s", src);
|
||||||
dst[len] = 0;
|
*dst = strtoul(tmp, &end, 16);
|
||||||
*size = len;
|
if (*end) {
|
||||||
|
*dst = 0;
|
||||||
|
*size = (uint32_t)(dst - head);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
left -= read_lim;
|
||||||
|
src += read_lim;
|
||||||
|
dst++;
|
||||||
}
|
}
|
||||||
dst[len] = 0;
|
*dst = 0;
|
||||||
*size = len;
|
*size = (uint32_t)(dst - head);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user