[PATCH 2/2] strtol.3: EXAMPLES: Fix error checking
Alejandro Colomar
alx@kernel.org
Thu Nov 30 15:30:26 GMT 2023
If strtol(3) returns 0 and sets errno to something different than
EINVAL, the call succeeded, or so we interpret from the standards.
POSIX allows libc functions to set errno in success, and it only
specifies two errors for strtol(3) for which it can return 0:
- Unsupported base. (errno must be set to EINVAL.)
- No conversion performed. (errno might be set to EINVAL.)
If errno is anything else, POSIX doesn't specify, so it can only be a
successful call strtol(3) that read "0" and set errno for spurious
reasons that are allowed.
Cc: Florian Weimer <fweimer@redhat.com>
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
man3/strtol.3 | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/man3/strtol.3 b/man3/strtol.3
index a5082a761..be8cc81d9 100644
--- a/man3/strtol.3
+++ b/man3/strtol.3
@@ -264,7 +264,8 @@ .SS Program source
/* Check for various possible errors */
\&
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
- || (errno != 0 && val == 0)) {
+ || (errno == EINVAL && val == 0))
+ {
perror("strtol");
exit(EXIT_FAILURE);
}
--
2.42.0
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/libc-help/attachments/20231130/c0b5ba16/attachment.sig>
More information about the Libc-help
mailing list