* [PATCH] app/testpmd: fix hex string parser input length
@ 2021-11-23 9:00 Gregory Etelson
2021-11-23 11:57 ` [PATCH v2] " Gregory Etelson
2021-11-24 12:33 ` [PATCH v3] " Gregory Etelson
0 siblings, 2 replies; 4+ messages in thread
From: Gregory Etelson @ 2021-11-23 9:00 UTC (permalink / raw)
To: dev, getelson
Cc: rasland, Ferruh Yigit, stable, Ori Kam, Xiaoyun Li, Wei Zhao
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(-)
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;
}
--
2.34.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2] app/testpmd: fix hex string parser input length
2021-11-23 9:00 [PATCH] app/testpmd: fix hex string parser input length Gregory Etelson
@ 2021-11-23 11:57 ` Gregory Etelson
2021-11-24 12:33 ` [PATCH v3] " Gregory Etelson
1 sibling, 0 replies; 4+ messages in thread
From: Gregory Etelson @ 2021-11-23 11:57 UTC (permalink / raw)
To: dev, getelson; +Cc: Ferruh Yigit, stable, Ori Kam, Xiaoyun Li, Wei Zhao
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>
---
v2: return error after snprintf failure.
---
app/test-pmd/cmdline_flow.c | 27 ++++++++++++++-------------
1 file changed, 14 insertions(+), 13 deletions(-)
diff --git a/app/test-pmd/cmdline_flow.c b/app/test-pmd/cmdline_flow.c
index 1b00ae507b..dcf8ebec45 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,22 @@ 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 = *size - left;
return -1;
}
+ left -= read_lim;
+ src += read_lim;
+ dst++;
}
- dst[len] = 0;
- *size = len;
-
+ *dst = 0;
return 0;
}
--
2.34.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v3] app/testpmd: fix hex string parser input length
2021-11-23 9:00 [PATCH] app/testpmd: fix hex string parser input length Gregory Etelson
2021-11-23 11:57 ` [PATCH v2] " Gregory Etelson
@ 2021-11-24 12:33 ` Gregory Etelson
2021-11-24 16:37 ` Ferruh Yigit
1 sibling, 1 reply; 4+ messages in thread
From: Gregory Etelson @ 2021-11-24 12:33 UTC (permalink / raw)
To: dev, getelson
Cc: Ferruh Yigit, stable, Viacheslav Ovsiienko, Ori Kam, Xiaoyun Li,
Wei Zhao
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
Cc: stable@dpdk.org
Fixes: 169a9fed1f4c ("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>
---
v2: Return error after snprintf failure.
v3: Update returned size.
Update the patch comment.
---
app/test-pmd/cmdline_flow.c | 29 ++++++++++++++++-------------
1 file changed, 16 insertions(+), 13 deletions(-)
diff --git a/app/test-pmd/cmdline_flow.c b/app/test-pmd/cmdline_flow.c
index 1b00ae507b..bbe3dc0115 100644
--- a/app/test-pmd/cmdline_flow.c
+++ b/app/test-pmd/cmdline_flow.c
@@ -7702,9 +7702,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) ||
@@ -7714,19 +7713,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;
}
--
2.34.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v3] app/testpmd: fix hex string parser input length
2021-11-24 12:33 ` [PATCH v3] " Gregory Etelson
@ 2021-11-24 16:37 ` Ferruh Yigit
0 siblings, 0 replies; 4+ messages in thread
From: Ferruh Yigit @ 2021-11-24 16:37 UTC (permalink / raw)
To: Gregory Etelson, dev
Cc: stable, Viacheslav Ovsiienko, Ori Kam, Xiaoyun Li, Wei Zhao
On 11/24/2021 12:33 PM, Gregory Etelson wrote:
> 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
>
> Cc: stable@dpdk.org
>
> Fixes: 169a9fed1f4c ("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>
Applied to dpdk-next-net/main, thanks.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-11-24 16:57 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-23 9:00 [PATCH] app/testpmd: fix hex string parser input length Gregory Etelson
2021-11-23 11:57 ` [PATCH v2] " Gregory Etelson
2021-11-24 12:33 ` [PATCH v3] " Gregory Etelson
2021-11-24 16:37 ` Ferruh Yigit
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).