Skip to content

Commit 032e084

Browse files
devnexenhtejun
authored andcommitted
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>
1 parent 749989b commit 032e084

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

tools/sched_ext/include/scx/compat.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ static inline long scx_hotplug_seq(void)
125125
{
126126
int fd;
127127
char buf[32];
128+
char *endptr;
128129
ssize_t len;
129130
long val;
130131

@@ -137,8 +138,10 @@ static inline long scx_hotplug_seq(void)
137138
buf[len] = 0;
138139
close(fd);
139140

140-
val = strtoul(buf, NULL, 10);
141-
SCX_BUG_ON(val < 0, "invalid num hotplug events: %lu", val);
141+
errno = 0;
142+
val = strtoul(buf, &endptr, 10);
143+
SCX_BUG_ON(errno == ERANGE || endptr == buf ||
144+
(*endptr != '\n' && *endptr != '\0'), "invalid num hotplug events: %ld", val);
142145

143146
return val;
144147
}

0 commit comments

Comments
 (0)