]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib,cpuset: enforce stricter parsing of cpu lists
authorHeiko Carstens <heiko.carstens@de.ibm.com>
Fri, 9 Sep 2011 09:19:33 +0000 (11:19 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 9 Sep 2011 22:00:33 +0000 (00:00 +0200)
The current cpulist_parse() function ignores extra non-parsable characters at
the end of the to be parsed cpu list string.  E.g. it would accept something
like "0bla" and just set bit 0 in the cpu set.  Since such a string is invalid
implement stricter parsing that makes sure that everything of the string has
been succesfully parsed.

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
lib/cpuset.c

index 85927553ee6b386411cea9297e21cd7c729021e3..ebaffccfb3bc85cefe932c53e6a0f0e61d34e199 100644 (file)
@@ -268,8 +268,9 @@ int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail)
 {
        size_t max = cpuset_nbits(setsize);
        const char *p, *q;
-       q = str;
+       int r;
 
+       q = str;
        CPU_ZERO_S(setsize, set);
 
        while (p = q, q = nexttoken(q, ','), p) {
@@ -277,8 +278,9 @@ int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail)
                unsigned int b; /* end of range */
                unsigned int s; /* stride */
                const char *c1, *c2;
+               char c;
 
-               if (sscanf(p, "%u", &a) < 1)
+               if ((r = sscanf(p, "%u%c", &a, &c)) < 1)
                        return 1;
                b = a;
                s = 1;
@@ -286,11 +288,11 @@ int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail)
                c1 = nexttoken(p, '-');
                c2 = nexttoken(p, ',');
                if (c1 != NULL && (c2 == NULL || c1 < c2)) {
-                       if (sscanf(c1, "%u", &b) < 1)
+                       if ((r = sscanf(c1, "%u%c", &b, &c)) < 1)
                                return 1;
                        c1 = nexttoken(c1, ':');
                        if (c1 != NULL && (c2 == NULL || c1 < c2)) {
-                               if (sscanf(c1, "%u", &s) < 1)
+                               if ((r = sscanf(c1, "%u%c", &s, &c)) < 1)
                                        return 1;
                                if (s == 0)
                                        return 1;
@@ -307,6 +309,8 @@ int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail)
                }
        }
 
+       if (r == 2)
+               return 1;
        return 0;
 }