* [PATCH] tools/sched_ext: fix strtoul() misuse in scx_hotplug_seq()
@ 2026-02-27 18:14 David Carlier
2026-02-27 18:20 ` Tejun Heo
0 siblings, 1 reply; 4+ messages in thread
From: David Carlier @ 2026-02-27 18:14 UTC (permalink / raw)
To: Tejun Heo, David Vernet; +Cc: linux-kernel, David Carlier
scx_hotplug_seq() uses strtoul() to parse the hotplug sequence number
but checks the result with val < 0. Since strtoul() returns an unsigned
long, it never produces a negative value, making the validation dead
code.
Switch to strtol() so the negative check is meaningful and fix the
format specifier from %lu to %ld to match the long type of val.
Signed-off-by: David Carlier <devnexen@gmail.com>
---
tools/sched_ext/include/scx/compat.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tools/sched_ext/include/scx/compat.h b/tools/sched_ext/include/scx/compat.h
index 8b4897fc8b99..1518d05d3e21 100644
--- a/tools/sched_ext/include/scx/compat.h
+++ b/tools/sched_ext/include/scx/compat.h
@@ -137,8 +137,8 @@ static inline long scx_hotplug_seq(void)
buf[len] = 0;
close(fd);
- val = strtoul(buf, NULL, 10);
- SCX_BUG_ON(val < 0, "invalid num hotplug events: %lu", val);
+ val = strtol(buf, NULL, 10);
+ SCX_BUG_ON(val < 0, "invalid num hotplug events: %ld", val);
return val;
}
--
2.51.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] tools/sched_ext: fix strtoul() misuse in scx_hotplug_seq()
2026-02-27 18:14 David Carlier
@ 2026-02-27 18:20 ` Tejun Heo
0 siblings, 0 replies; 4+ messages in thread
From: Tejun Heo @ 2026-02-27 18:20 UTC (permalink / raw)
To: David Carlier; +Cc: David Vernet, linux-kernel
On Fri, Feb 27, 2026 at 06:14:39PM +0000, David Carlier wrote:
> + val = strtol(buf, NULL, 10);
> + SCX_BUG_ON(val < 0, "invalid num hotplug events: %ld", val);
This isn't a proper way to detect conversion errors either, right? Wouldn't
it be better to keep strtoul() and test whether endptr reaches \0?
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH] tools/sched_ext: fix strtoul() misuse in scx_hotplug_seq()
@ 2026-02-27 18:43 David Carlier
2026-02-27 19:19 ` Tejun Heo
0 siblings, 1 reply; 4+ messages in thread
From: David Carlier @ 2026-02-27 18:43 UTC (permalink / raw)
To: Tejun Heo, David Vernet; +Cc: linux-kernel, David Carlier
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>
---
tools/sched_ext/include/scx/compat.h | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/tools/sched_ext/include/scx/compat.h b/tools/sched_ext/include/scx/compat.h
index 8b4897fc8b99..edccc99c7294 100644
--- a/tools/sched_ext/include/scx/compat.h
+++ b/tools/sched_ext/include/scx/compat.h
@@ -125,6 +125,7 @@ static inline long scx_hotplug_seq(void)
{
int fd;
char buf[32];
+ char *endptr;
ssize_t len;
long val;
@@ -137,8 +138,10 @@ static inline long scx_hotplug_seq(void)
buf[len] = 0;
close(fd);
- val = strtoul(buf, NULL, 10);
- SCX_BUG_ON(val < 0, "invalid num hotplug events: %lu", val);
+ errno = 0;
+ val = strtoul(buf, &endptr, 10);
+ SCX_BUG_ON(errno == ERANGE || endptr == buf ||
+ (*endptr != '\n' && *endptr != '\0'), "invalid num hotplug events: %ld", val);
return val;
}
--
2.51.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] tools/sched_ext: fix strtoul() misuse in scx_hotplug_seq()
2026-02-27 18:43 [PATCH] tools/sched_ext: fix strtoul() misuse in scx_hotplug_seq() David Carlier
@ 2026-02-27 19:19 ` Tejun Heo
0 siblings, 0 replies; 4+ messages in thread
From: Tejun Heo @ 2026-02-27 19:19 UTC (permalink / raw)
To: David Carlier, David Vernet; +Cc: linux-kernel, sched-ext, Emil Tsalapatis
Applied to sched_ext/for-7.0-fixes.
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-02-27 19:19 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-27 18:43 [PATCH] tools/sched_ext: fix strtoul() misuse in scx_hotplug_seq() David Carlier
2026-02-27 19:19 ` Tejun Heo
-- strict thread matches above, loose matches on Subject: below --
2026-02-27 18:14 David Carlier
2026-02-27 18:20 ` Tejun Heo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox