public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/2] Revert "strtol.3: EXAMPLES: Simplify errno checking"
@ 2023-11-30 15:29 Alejandro Colomar
  2023-11-30 15:30 ` [PATCH 2/2] strtol.3: EXAMPLES: Fix error checking Alejandro Colomar
  2023-11-30 23:07 ` [PATCH v2] strtol.3, strtoul.3: ERRORS: Clarify that these don't set errno on success Alejandro Colomar
  0 siblings, 2 replies; 3+ messages in thread
From: Alejandro Colomar @ 2023-11-30 15:29 UTC (permalink / raw)
  To: linux-man
  Cc: Alejandro Colomar, libc-help, ~hallyn/shadow, Michael Kerrisk,
	Florian Weimer, Iker Pedrosa

[-- Attachment #1: Type: text/plain, Size: 1216 bytes --]

This reverts commit 93f369892aeab4d56b92962224e318f739ee2455.

That commit was wrong.  It is necessary to check both the return value
_and_ errno to determine that strtol(3) failed.  In fact, the checks
are still slightly incorrect, since strtol(3) could succeed and
return 0, but still set errno, to something other than EINVAL.

Link: <https://lore.kernel.org/linux-man/ZWiCsBkRpOLEc1Y3@debian/T/#t>
Cc: Florian Weimer <fweimer@redhat.com>
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
 man3/strtol.3 | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/man3/strtol.3 b/man3/strtol.3
index 01c658025..a5082a761 100644
--- a/man3/strtol.3
+++ b/man3/strtol.3
@@ -261,9 +261,10 @@ .SS Program source
     errno = 0;    /* To distinguish success/failure after call */
     val = strtol(str, &endptr, base);
 \&
-    /* Check for various possible errors. */
+    /* Check for various possible errors */
 \&
-    if (errno != 0) {
+    if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
+            || (errno != 0 && val == 0)) {
         perror("strtol");
         exit(EXIT_FAILURE);
     }
-- 
2.42.0


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-11-30 23:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-30 15:29 [PATCH 1/2] Revert "strtol.3: EXAMPLES: Simplify errno checking" Alejandro Colomar
2023-11-30 15:30 ` [PATCH 2/2] strtol.3: EXAMPLES: Fix error checking Alejandro Colomar
2023-11-30 23:07 ` [PATCH v2] strtol.3, strtoul.3: ERRORS: Clarify that these don't set errno on success Alejandro Colomar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).