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
This commit is contained in:
Sergey Kandaurov 2013-08-21 22:37:15 +00:00
parent 5188b5f3c2
commit 7261b203c6

View File

@ -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;