From eb594251b6d1a06d2acbb281b3816c017d7066eb Mon Sep 17 00:00:00 2001 From: Iustin Pop Date: Wed, 11 Jun 2008 17:55:18 +0200 Subject: [PATCH] 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 --- misc/badblocks.c | 1 + 1 file changed, 1 insertion(+) 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)) { -- 2.47.3