From: yeeli <[email protected]>

util_bus_filter(), util_region_filter() and util_dimm_filter() split
the identifier string with strtok_r(), but then parse "ident" instead
of the current token "name" with strtoul().  ident is the start of
the strdup()ed buffer, so every iteration parses the first token
again.

A numeric list therefore only matches its first entry.  For example,
with a dimm whose id is 1:

    util_dimm_filter(dimm, "0 1")    -> no match
    util_dimm_filter(dimm, "1")      -> match

The same happens for --bus, --region and --dimm lists; name lists
such as "nmem0 nmem1" still work because they take the string
comparison path.

Reproduce with a small harness that calls util_dimm_filter() with a
dimm named nmem1 and id 1:

    "0 1"    -> no match (expected match)
    "0 2 1"  -> no match (expected match)

Parse the current token with strtoul(name, ...), and compare the end
pointer with name when checking for a failed conversion.

Tested on the target test system with harnesses for all three
functions: the original code fails for "0 1" and "1 0" as expected,
and the patched code matches for numeric lists, hex ids, name lists,
"all" and rejects trailing junk.

The bus and region helpers have had the same bug since 56b6e85
("ndctl: refactor bus and region filtering to utility routines").

Fixes: 4c9185d60c0b ("ndctl, list: add 'filter by dimm' capability")
Signed-off-by: yeeli <[email protected]>
---
 ndctl/filter.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/ndctl/filter.c b/ndctl/filter.c
index 5b810dc..e6e9aba 100644
--- a/ndctl/filter.c
+++ b/ndctl/filter.c
@@ -31,8 +31,8 @@ struct ndctl_bus *util_bus_filter(struct ndctl_bus *bus, 
const char *__ident)
                if (strcmp(name, "all") == 0)
                        break;
 
-               bus_id = strtoul(ident, &end, 0);
-               if (end == ident || end[0])
+               bus_id = strtoul(name, &end, 0);
+               if (end == name || end[0])
                        bus_id = ULONG_MAX;
 
                provider = ndctl_bus_get_provider(bus);
@@ -72,8 +72,8 @@ struct ndctl_region *util_region_filter(struct ndctl_region 
*region,
                if (strcmp(name, "all") == 0)
                        break;
 
-               region_id = strtoul(ident, &end, 0);
-               if (end == ident || end[0])
+               region_id = strtoul(name, &end, 0);
+               if (end == name || end[0])
                        region_id = ULONG_MAX;
 
                region_name = ndctl_region_get_devname(region);
@@ -146,8 +146,8 @@ struct ndctl_dimm *util_dimm_filter(struct ndctl_dimm *dimm,
                if (strcmp(name, "all") == 0)
                        break;
 
-               dimm_id = strtoul(ident, &end, 0);
-               if (end == ident || end[0])
+               dimm_id = strtoul(name, &end, 0);
+               if (end == name || end[0])
                        dimm_id = ULONG_MAX;
 
                dimm_name = ndctl_dimm_get_devname(dimm);
-- 
2.34.1


Reply via email to