From f27ce0711cb9b2611a4bf679f83607439462cbb0 Mon Sep 17 00:00:00 2001 From: Heiko Carstens Date: Fri, 9 Sep 2011 11:19:33 +0200 Subject: 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 --- lib/cpuset.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'lib/cpuset.c') diff --git a/lib/cpuset.c b/lib/cpuset.c index 85927553e..ebaffccfb 100644 --- a/lib/cpuset.c +++ b/lib/cpuset.c @@ -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; } -- cgit v1.2.3-55-g7522