tools/power turbostat: Use strtoul() for iteration parsing
PATCH v2 1/1 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 <[email protected]>
---
v2: Use strtoul() + errno instead of strtod() with signed validation per review feedback
tools/power/x86/turbostat/turbostat.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
tools/power/x86/turbostat/turbostat.c +8 -6
@@ -11160,18 +11160,20 @@ void cmdline(int argc, char **argv)/* Parsed earlier */ break ;case 'n' : - num_iterations =strtod (optarg,NULL ); + num_iterations =strtoul (optarg,NULL ,0 ); + errno =0 ; -if (num_iterations <=0 ) { -fprintf (outf,"iterations %d should be positive number\n" , num_iterations); +if (errno || num_iterations ==0 ) { +fprintf (outf,"invalid iteration count: %s\n" , optarg);exit (2 ); }break ;case 'N' : - header_iterations =strtod (optarg,NULL ); + header_iterations =strtoul (optarg,NULL ,0 ); + errno =0 ; -if (header_iterations <=0 ) { -fprintf (outf,"iterations %d should be positive number\n" , header_iterations); +if (errno || header_iterations ==0 ) { +fprintf (outf,"invalid header iteration count: %s\n" , optarg);exit (2 ); }break ;
Signature · 1 line
2.34.1