From: Iustin Pop Date: Wed, 11 Jun 2008 15:55:18 +0000 (+0200) Subject: badblocks: fix a possible bug in parse_uint X-Git-Tag: v1.41-WIP-0617~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eb594251b6d1a06d2acbb281b3816c017d7066eb;p=thirdparty%2Fe2fsprogs.git badblocks: fix a possible bug in parse_uint Currently, the parse_uint() function checks errno after the strtoul() call. But, according to the man page of strtoul(): Since strtoul() can legitimately return 0 or LONG_MAX (LLONG_MAX for strtoull()) on both success and failure, the calling program should set errno to 0 before the call, and then determine if an error occurred by checking whether errno has a nonzero value after the call. When using locales, it can happen that looking for the locale files is not successful, and therefore errno will have a nonzero value from this. And since the argument parsing is one of the first things done after startup, parse_uint() will wrongly report errors. The fix is to simply reset errno to zero before calling strtoul(). Signed-off-by: Iustin Pop Signed-off-by: Theodore Ts'o --- diff --git a/misc/badblocks.c b/misc/badblocks.c index d2992e364..5676a8486 100644 --- a/misc/badblocks.c +++ b/misc/badblocks.c @@ -853,6 +853,7 @@ static unsigned int parse_uint(const char *str, const char *descr) char *tmp; unsigned long ret; + errno = 0; ret = strtoul(str, &tmp, 0); if (*tmp || errno || (ret > UINT_MAX) || (ret == ULONG_MAX && errno == ERANGE)) {