Skip to contentSkip to conversation

tools/power turbostat: Refine iteration parsing

PATCH In review Kaushlendra Kumar 2 participants 3 messages Dec 1, 2025 – Dec 5, 2025 Turbostat utility

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/power turbostat: Refine iteration parsing

Validate the -n/-N arguments as signed ints before casting to unsigned,so the user sees the correct value in error paths and negative input is rejected cleanly.

---
 tools/power/x86/turbostat/turbostat.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
tools/power/x86/turbostat/turbostat.c +8 -6
index f2512d78bcbd..807b2d7f1809 100644
@@ -11123,20 +11123,22 @@ void cmdline(int argc, char **argv)
 			/* Parsed earlier */
 			break;
 		case 'n':
-			num_iterations = strtod(optarg, NULL);
+			int iterations = strtod(optarg, NULL);
 
-			if (num_iterations <= 0) {
-				fprintf(outf, "iterations %d should be positive number\n", num_iterations);
+			if (iterations <= 0) {
+				fprintf(outf, "iterations %d should be positive number\n", iterations);
 				exit(2);
 			}
+			num_iterations = (unsigned int)iterations;
 			break;
 		case 'N':
-			header_iterations = strtod(optarg, NULL);
+			int h_iterations = strtod(optarg, NULL);
 
-			if (header_iterations <= 0) {
-				fprintf(outf, "iterations %d should be positive number\n", header_iterations);
+			if (h_iterations <= 0) {
+				fprintf(outf, "iterations %d should be positive number\n", h_iterations);
 				exit(2);
 			}
+			header_iterations = (unsigned int)h_iterations;
 			break;
 		case 's':
 			/*
Signature · 1 line
2.34.1
maintainer
#1 lore raw

Looks like the code tweaked in this patch and the next was a result of copy/paste of correct code use of strtod().

But here the result is simply an unsigned long counter, so we should probably be just using strtoul() and checking errno.
In the unlikely event somebody sends a negative value through, they'll get a big positive number -- fine.

thanks,
-Len

Quoted 46 lines · Kaushlendra Kumar

Validate the -n/-N arguments as signed ints before casting to unsigned,so the user sees the correct value in error paths and negative input is rejected cleanly.

---
 tools/power/x86/turbostat/turbostat.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
tools/power/x86/turbostat/turbostat.c +8 -7
index f2512d78bcbd..807b2d7f1809 100644
@@ -11123,20 +11123,22 @@ void cmdline(int argc, char **argv)
                        /* Parsed earlier */
                        break;
                case 'n':
-                       num_iterations = strtod(optarg, NULL);
+                       int iterations = strtod(optarg, NULL);
 
-                       if (num_iterations <= 0) {
-                               fprintf(outf, "iterations %d should be positive number\n", num_iterations);
+                       if (iterations <= 0) {
+                               fprintf(outf, "iterations %d should be positive number\n", iterations);
                                exit(2);
                        }
+                       num_iterations = (unsigned int)iterations;
                        break;
                case 'N':
-                       header_iterations = strtod(optarg, NULL);
+                       int h_iterations = strtod(optarg, NULL);
 
-                       if (header_iterations <= 0) {
-                               fprintf(outf, "iterations %d should be positive number\n", header_iterations);
+                       if (h_iterations <= 0) {
+                               fprintf(outf, "iterations %d should be positive number\n", h_iterations);
                                exit(2);
                        }
+                       header_iterations = (unsigned int)h_iterations;
                        break;
                case 's':
                        /*
--

2.34.1

Signature · 1 line
Len Brown, Intel Open Source Technology Center
#2 lore raw
Quoted 7 lines · Len Brown

Looks like the code tweaked in this patch and the next was a result of copy/paste of correct code use of strtod().

But here the result is simply an unsigned long counter, so we should probably be just using strtoul() and checking errno.
In the unlikely event somebody sends a negative value through, they'll get a big positive number -- fine.

Thanks, Len. Agreed — strtoul() with errno checking is the right fit here.

Will send a v2 accordingly.

—Kaushlendra