[v2,05/10] cmdline: fix port list parsing
Checks
Commit Message
Doing arithmetics with the NULL pointer is undefined.
Caught by UBSan:
../lib/cmdline/cmdline_parse_portlist.c:40:19: runtime error:
applying non-zero offset 1 to null pointer
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior
../lib/cmdline/cmdline_parse_portlist.c:40:19 in
Fixes: af75078fece3 ("first public release")
Cc: stable@dpdk.org
Signed-off-by: David Marchand <david.marchand@redhat.com>
---
Changes since v1:
- moved some variable as suggested by Bruce,
---
lib/cmdline/cmdline_parse_portlist.c | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)
Comments
On Mon, Jun 23, 2025 at 03:52:35PM +0200, David Marchand wrote:
> Doing arithmetics with the NULL pointer is undefined.
>
> Caught by UBSan:
>
> ../lib/cmdline/cmdline_parse_portlist.c:40:19: runtime error:
> applying non-zero offset 1 to null pointer
> SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior
> ../lib/cmdline/cmdline_parse_portlist.c:40:19 in
>
> Fixes: af75078fece3 ("first public release")
> Cc: stable@dpdk.org
>
> Signed-off-by: David Marchand <david.marchand@redhat.com>
> ---
> Changes since v1:
> - moved some variable as suggested by Bruce,
>
Acked-by: Bruce Richardson <bruce.richardson@intel.com>
Acked-by: Marat Khalili <marat.khalili@huawei.com>
@@ -33,15 +33,12 @@ parse_set_list(cmdline_portlist_t *pl, size_t low, size_t high)
static int
parse_ports(cmdline_portlist_t *pl, const char *str)
{
+ const char *first = str;
size_t ps, pe;
- const char *first, *last;
char *end;
- for (first = str, last = first;
- first != NULL && last != NULL;
- first = last + 1) {
-
- last = strchr(first, ',');
+ while (first != NULL) {
+ const char *last = strchr(first, ',');
errno = 0;
ps = strtoul(first, &end, 10);
@@ -65,6 +62,7 @@ parse_ports(cmdline_portlist_t *pl, const char *str)
return -1;
parse_set_list(pl, ps, pe);
+ first = (last == NULL ? NULL : last + 1);
}
return 0;