Skip to contentSkip to conversation

rtla: Fix parse_cpu_set() bug introduced by strtoi()

PATCH In review Costa Shulyupin 2 participants 2 messages Jan 12 – Jan 13

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 rtla: Fix parse_cpu_set() bug introduced by strtoi()

The patch 'Replace atoi() with a robust strtoi()' introduced a bug in parse_cpu_set(), which relies on partial parsing of the input string.

The function parses CPU specifications like '0-3,5' by incrementing a pointer through the string. strtoi() rejects strings with trailing characters, causing parse_cpu_set() to fail on any CPU list with multiple entries.

Restore the original use of atoi() in parse_cpu_set().

Fixes 7e9dfccf8f11 ("rtla: Replace atoi() with a robust strtoi()")

---

v2:
- Split the patch.

---
 tools/tracing/rtla/src/utils.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
tools/tracing/rtla/src/utils.c +4 -6
index 18986a5aed3c..0da3b2470c31 100644
@@ -128,18 +128,16 @@ int parse_cpu_set(char *cpu_list, cpu_set_t *set)
 	nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
 
 	for (p = cpu_list; *p; ) {
-		if (strtoi(p, &cpu))
-			goto err;
-		if (cpu < 0 || cpu >= nr_cpus)
+		cpu = atoi(p);
+		if (cpu < 0 || (!cpu && *p != '0') || cpu >= nr_cpus)
 			goto err;
 
 		while (isdigit(*p))
 			p++;
 		if (*p == '-') {
 			p++;
-			if (strtoi(p, &end_cpu))
-				goto err;
-			if (end_cpu < cpu || end_cpu >= nr_cpus)
+			end_cpu = atoi(p);
+			if (end_cpu < cpu || (!end_cpu && *p != '0') || end_cpu >= nr_cpus)
 				goto err;
 			while (isdigit(*p))
 				p++;
Signature · 1 line
2.52.0
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 lines · On Mon, 12 Jan 2026 21:26:41 +0200 Costa Shulyupin <[email protected]>…

The patch 'Replace atoi() with a robust strtoi()' introduced a bug in parse_cpu_set(), which relies on partial parsing of the input string.

The function parses CPU specifications like '0-3,5' by incrementing a pointer through the string. strtoi() rejects strings with trailing characters, causing parse_cpu_set() to fail on any CPU list with multiple entries.

Quoted 4 lines

Restore the original use of atoi() in parse_cpu_set().

Fixes 7e9dfccf8f11 ("rtla: Replace atoi() with a robust strtoi()")

Looks good to me.

Thanks,

Quoted 40 lines

---

v2:
- Split the patch.

---
 tools/tracing/rtla/src/utils.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
tools/tracing/rtla/src/utils.c +4 -6
index 18986a5aed3c..0da3b2470c31 100644
@@ -128,18 +128,16 @@ int parse_cpu_set(char *cpu_list, cpu_set_t *set)
 	nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
 
 	for (p = cpu_list; *p; ) {
-		if (strtoi(p, &cpu))
-			goto err;
-		if (cpu < 0 || cpu >= nr_cpus)
+		cpu = atoi(p);
+		if (cpu < 0 || (!cpu && *p != '0') || cpu >= nr_cpus)
 			goto err;
 
 		while (isdigit(*p))
 			p++;
 		if (*p == '-') {
 			p++;
-			if (strtoi(p, &end_cpu))
-				goto err;
-			if (end_cpu < cpu || end_cpu >= nr_cpus)
+			end_cpu = atoi(p);
+			if (end_cpu < cpu || (!end_cpu && *p != '0') || end_cpu >= nr_cpus)
 				goto err;
 			while (isdigit(*p))
 				p++;
Signature · 1 line
2.52.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>