Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools/sched_ext: fix strtoul() misuse in scx_hotplug_seq()

scx_hotplug_seq() uses strtoul() but validates the result with a
negative check (val < 0), which can never be true for an unsigned
return value.

Use the endptr mechanism to verify the entire string was consumed,
and check errno == ERANGE for overflow detection.

Signed-off-by: David Carlier <devnexen@gmail.com>
Signed-off-by: Tejun Heo <tj@kernel.org>

authored by

David Carlier and committed by
Tejun Heo
032e084f 749989b2

+5 -2
+5 -2
tools/sched_ext/include/scx/compat.h
··· 125 125 { 126 126 int fd; 127 127 char buf[32]; 128 + char *endptr; 128 129 ssize_t len; 129 130 long val; 130 131 ··· 138 137 buf[len] = 0; 139 138 close(fd); 140 139 141 - val = strtoul(buf, NULL, 10); 142 - SCX_BUG_ON(val < 0, "invalid num hotplug events: %lu", val); 140 + errno = 0; 141 + val = strtoul(buf, &endptr, 10); 142 + SCX_BUG_ON(errno == ERANGE || endptr == buf || 143 + (*endptr != '\n' && *endptr != '\0'), "invalid num hotplug events: %ld", val); 143 144 144 145 return val; 145 146 }