app/testpmd: fix hex string parser input length

Message ID 20211123090005.24707-1-getelson@nvidia.com (mailing list archive)
State Superseded, archived
Delegated to: Ferruh Yigit
Headers
Series app/testpmd: fix hex string parser input length |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/github-robot: build success github build: passed
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS

Commit Message

Gregory Etelson Nov. 23, 2021, 9 a.m. UTC
  Current hex string parser assumes input has even characters number.
The parser fails input string with odd length.

The patch parses hex stringhs with even and odd length.

Cc: stable@dpdk.org

Fixes: 169a9fed1f4c ("app/testpmd: fix hex string parser support for flow API")
Signed-off-by: Gregory Etelson <getelson@nvidia.com>
---
 app/test-pmd/cmdline_flow.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
  

Patch

diff --git a/app/test-pmd/cmdline_flow.c b/app/test-pmd/cmdline_flow.c
index 1b00ae507b..2b90a4a23a 100644
--- a/app/test-pmd/cmdline_flow.c
+++ b/app/test-pmd/cmdline_flow.c
@@ -7702,9 +7702,7 @@  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;
 
 	/* Check input parameters */
 	if ((src == NULL) ||
@@ -7714,19 +7712,21 @@  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;
-			return -1;
+	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 = *size - left;
 		}
+		left -= read_lim;
+		src += read_lim;
+		dst++;
 	}
-	dst[len] = 0;
-	*size = len;
-
+	*dst = 0;
 	return 0;
 }