Skip to content

Commit

Permalink
app/testpmd: fix hexadecimal parser with odd length
Browse files Browse the repository at this point in the history
[ upstream commit ea1da43 ]

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: 169a9fe ("app/testpmd: fix hex string parser support for flow API")

Signed-off-by: Gregory Etelson <getelson@nvidia.com>
Reviewed-by: Viacheslav Ovsiienko <viacheslavo@nvidia.com>
  • Loading branch information
getelson-at-mellanox authored and cpaelzer committed Nov 30, 2021
1 parent e495c93 commit 86e6d75
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions app/test-pmd/cmdline_flow.c
Expand Up @@ -5293,9 +5293,8 @@ parse_string(struct context *ctx, const struct token *token,
static int
parse_hex_string(const char *src, uint8_t *dst, uint32_t *size)
{
char *c = NULL;
uint32_t i, len;
char tmp[3];
uint32_t left = *size;
const uint8_t *head = dst;

/* Check input parameters */
if ((src == NULL) ||
Expand All @@ -5305,19 +5304,23 @@ parse_hex_string(const char *src, uint8_t *dst, uint32_t *size)
return -1;

/* Convert chars to bytes */
for (i = 0, len = 0; i < *size; i += 2) {
snprintf(tmp, 3, "%s", src + i);
dst[len++] = strtoul(tmp, &c, 16);
if (*c != 0) {
len--;
dst[len] = 0;
*size = len;
while (left) {
char tmp[3], *end = tmp;
uint32_t read_lim = left & 1 ? 1 : 2;

snprintf(tmp, read_lim + 1, "%s", src);
*dst = strtoul(tmp, &end, 16);
if (*end) {
*dst = 0;
*size = (uint32_t)(dst - head);
return -1;
}
left -= read_lim;
src += read_lim;
dst++;
}
dst[len] = 0;
*size = len;

*dst = 0;
*size = (uint32_t)(dst - head);
return 0;
}

Expand Down

0 comments on commit 86e6d75

Please sign in to comment.