Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf util: Improve error checking for time percent input

The command line like 'perf report --stdio --time 1abc%/1' could be
accepted by perf. It looks not very good.

This patch uses strtod() to replace original atof() and check the entire
string. Now for the same command line, it would return error message
"Invalid time string".

root@skl:/tmp# perf report --stdio --time 1abc%/1
Invalid time string

Signed-off-by: Jin Yao <yao.jin@linux.intel.com>
Reviewed-by: Jiri Olsa <jolsa@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Kan Liang <kan.liang@intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/1515596433-24653-4-git-send-email-yao.jin@linux.intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Jin Yao and committed by
Arnaldo Carvalho de Melo
6e761cbc 1e2778e9

+6 -2
+6 -2
tools/perf/util/time-utils.c
··· 116 116 117 117 static int parse_percent(double *pcnt, char *str) 118 118 { 119 - char *c; 119 + char *c, *endptr; 120 + double d; 120 121 121 122 c = strchr(str, '%'); 122 123 if (c) ··· 125 124 else 126 125 return -1; 127 126 128 - *pcnt = atof(str) / 100.0; 127 + d = strtod(str, &endptr); 128 + if (endptr != str + strlen(str)) 129 + return -1; 129 130 131 + *pcnt = d / 100.0; 130 132 return 0; 131 133 } 132 134