diff options
author | Benjamin Gray <bgray@linux.ibm.com> | 2023-02-03 11:39:45 +1100 |
---|---|---|
committer | Michael Ellerman <mpe@ellerman.id.au> | 2023-02-09 23:56:45 +1100 |
commit | d1bc05b7bf02f8635fe6c445f67d78f85234cbb7 (patch) | |
tree | 85d9bd4f50840b97852992c69db196f6120e51f0 /tools/testing/selftests/powerpc/pmu | |
parent | 121d340be9a17ed89d523c56203908c01e09a306 (diff) | |
download | lwn-d1bc05b7bf02f8635fe6c445f67d78f85234cbb7.tar.gz lwn-d1bc05b7bf02f8635fe6c445f67d78f85234cbb7.zip |
selftests/powerpc: Parse long/unsigned long value safely
Often a file is expected to hold an integral value. Existing functions
will use a C stdlib function like atoi or strtol to parse the file.
These operations are error prone, with complicated error conditions
(atoi returns 0 if not a number, and is undefined behaviour if not in
range. strtol returns 0 if not a number, and LONG_MIN/MAX if not in
range + sets errno to ERANGE).
Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20230203003947.38033-4-bgray@linux.ibm.com
Diffstat (limited to 'tools/testing/selftests/powerpc/pmu')
-rw-r--r-- | tools/testing/selftests/powerpc/pmu/lib.c | 6 |
1 files changed, 2 insertions, 4 deletions
diff --git a/tools/testing/selftests/powerpc/pmu/lib.c b/tools/testing/selftests/powerpc/pmu/lib.c index 960915304a65..1cfc13a25aee 100644 --- a/tools/testing/selftests/powerpc/pmu/lib.c +++ b/tools/testing/selftests/powerpc/pmu/lib.c @@ -192,7 +192,6 @@ bool require_paranoia_below(int level) { int err; long current; - char *end; char buf[16] = {0}; err = read_file(PARANOID_PATH, buf, sizeof(buf) - 1, NULL); @@ -201,9 +200,8 @@ bool require_paranoia_below(int level) return false; } - current = strtol(buf, &end, 10); - - if (end == buf) { + err = parse_long(buf, sizeof(buf), ¤t, 10); + if (err) { printf("Couldn't parse " PARANOID_PATH "?\n"); return false; } |