Skip to content

Commit 8e5c0cc

Browse files
kaushlenlenb
authored andcommitted
tools/power turbostat: Use strtoul() for iteration parsing
Replace strtod() with strtoul() and check errno for -n/-N options, since num_iterations and header_iterations are unsigned long counters. Reject zero and conversion errors; negative inputs wrap to large positive values per standard unsigned semantics. Signed-off-by: Kaushlendra Kumar <kaushlendra.kumar@intel.com> Signed-off-by: Len Brown <len.brown@intel.com>
1 parent a2b4d0f commit 8e5c0cc

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

tools/power/x86/turbostat/turbostat.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11536,18 +11536,20 @@ void cmdline(int argc, char **argv)
1153611536
/* Parsed earlier */
1153711537
break;
1153811538
case 'n':
11539-
num_iterations = strtod(optarg, NULL);
11539+
num_iterations = strtoul(optarg, NULL, 0);
11540+
errno = 0;
1154011541

11541-
if (num_iterations <= 0) {
11542-
fprintf(outf, "iterations %d should be positive number\n", num_iterations);
11542+
if (errno || num_iterations == 0) {
11543+
fprintf(outf, "invalid iteration count: %s\n", optarg);
1154311544
exit(2);
1154411545
}
1154511546
break;
1154611547
case 'N':
11547-
header_iterations = strtod(optarg, NULL);
11548+
header_iterations = strtoul(optarg, NULL, 0);
11549+
errno = 0;
1154811550

11549-
if (header_iterations <= 0) {
11550-
fprintf(outf, "iterations %d should be positive number\n", header_iterations);
11551+
if (errno || header_iterations == 0) {
11552+
fprintf(outf, "invalid header iteration count: %s\n", optarg);
1155111553
exit(2);
1155211554
}
1155311555
break;

0 commit comments

Comments
 (0)