Skip to contentSkip to conversation

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

PATCH Applied David Carlier 2 participants 2 messages Feb 27 Scheduler - sched_ext

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 tools/sched_ext/include/scx/compat.h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/sched_ext/include/scx/compat.h +5 -2
index 8b4897fc8b99..edccc99c7294 100644
@@ -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;
 }
Signature · 1 line
2.51.0
reviewer Applied
#1 lore raw

Applied to sched_ext/for-7.0-fixes.

Thanks.

--
tejun