From 96dfe577d25fd7efedf4ae97d4a81f81ae7fd1c8 Mon Sep 17 00:00:00 2001 From: pluknet Date: Wed, 21 Aug 2013 22:37:15 +0000 Subject: Reset errno before strtoumax() call to properly detect ERANGE. Restore saved errno if strtoumax() call is successful. Reported by: ache Reviewed by: jilles MFC after: 1 week --- lib/libutil/expand_number.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/libutil/expand_number.c b/lib/libutil/expand_number.c index 0a62c12..401e2d9 100644 --- a/lib/libutil/expand_number.c +++ b/lib/libutil/expand_number.c @@ -50,15 +50,22 @@ int expand_number(const char *buf, uint64_t *num) { uint64_t number; + int saved_errno; unsigned shift; char *endptr; + saved_errno = errno; + errno = 0; + number = strtoumax(buf, &endptr, 0); if (number == UINTMAX_MAX && errno == ERANGE) { return (-1); } + if (errno == 0) + errno = saved_errno; + if (endptr == buf) { /* No valid digits. */ errno = EINVAL; -- cgit v1.1