lib/, src/: Fix error handling after strto[u]l[l](3)

-  Set errno = 0 before the call.  Otherwise, it may contain anything.
-  ERANGE is not the only possible errno value of these functions.  They
   can also set it to EINVAL.
-  Any errno value after these calls is bad; just compare against 0.
-  Don't check for the return value; just errno.  This function is
   guaranteed to not modify errno on success (POSIX).
-  Check endptr == str, which may or may not set EINVAL.

Suggested-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2023-12-01 19:31:16 +01:00
committed by Iker Pedrosa
parent f6701d3efa
commit 1c464d9a2d
12 changed files with 32 additions and 34 deletions
+3 -2
View File
@@ -34,11 +34,12 @@ int main(int argc, char **argv)
owner = argv[1];
check_uids = argv[2][0] == 'u';
errno = 0;
start = strtoul(argv[3], NULL, 10);
if (start == ULONG_MAX && errno == ERANGE)
if (errno != 0)
exit(1);
count = strtoul(argv[4], NULL, 10);
if (count == ULONG_MAX && errno == ERANGE)
if (errno != 0)
exit(1);
if (check_uids) {
if (have_sub_uids(owner, start, count))
+2 -2
View File
@@ -318,13 +318,13 @@ static struct ulong_range getulong_range(const char *str)
errno = 0;
first = strtoll(str, &pos, 10);
if (('\0' == *str) || ('-' != *pos ) || (ERANGE == errno) ||
if (('\0' == *str) || ('-' != *pos ) || (0 != errno) ||
(first != (unsigned long)first))
goto out;
errno = 0;
last = strtoll(pos + 1, &pos, 10);
if (('\0' != *pos ) || (ERANGE == errno) ||
if (('\0' != *pos ) || (0 != errno) ||
(last != (unsigned long)last))
goto out;