Whamcloud - gitweb
badblocks: fix a possible bug in parse_uint
authorIustin Pop <iustin@google.com>
Wed, 11 Jun 2008 15:55:18 +0000 (17:55 +0200)
committerTheodore Ts'o <tytso@mit.edu>
Tue, 17 Jun 2008 11:33:44 +0000 (07:33 -0400)
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 <iustin@google.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
misc/badblocks.c

index d2992e3..5676a84 100644 (file)
@@ -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)) {