lib,cpuset: enforce stricter parsing of cpu lists

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>
This commit is contained in:
Heiko Carstens 2011-09-09 11:19:33 +02:00 committed by Karel Zak
parent 72232a267a
commit f27ce0711c
1 changed files with 8 additions and 4 deletions

View 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;
}