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

* [PATCH 2/2] strtol.3: EXAMPLES: Fix error checking
  2023-11-30 15:29 [PATCH 1/2] Revert "strtol.3: EXAMPLES: Simplify errno checking" Alejandro Colomar
@ 2023-11-30 15:30 ` 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
  1 sibling, 0 replies; 3+ messages in thread
From: Alejandro Colomar @ 2023-11-30 15:30 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: 1224 bytes --]

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


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

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

* [PATCH v2] strtol.3, strtoul.3: ERRORS: Clarify that these don't set errno on success
  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 ` Alejandro Colomar
  1 sibling, 0 replies; 3+ messages in thread
From: Alejandro Colomar @ 2023-11-30 23:07 UTC (permalink / raw)
  To: linux-man
  Cc: Alejandro Colomar, libc-help, ~hallyn/shadow, Jakub Wilk,
	Florian Weimer, Iker Pedrosa, Michael Kerrisk

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

That's a guarantee made by POSIX.

Reported-by: Jakub Wilk <jwilk@jwilk.net>
Cc: Florian Weimer <fweimer@redhat.com>
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
 man3/strtol.3  | 3 +++
 man3/strtoul.3 | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/man3/strtol.3 b/man3/strtol.3
index 01c658025..1f1f98216 100644
--- a/man3/strtol.3
+++ b/man3/strtol.3
@@ -124,6 +124,9 @@ .SH RETURN VALUE
 and
 .BR LONG_MAX ).
 .SH ERRORS
+This function does not modify
+.I errno
+on success.
 .TP
 .B EINVAL
 (not in C99)
diff --git a/man3/strtoul.3 b/man3/strtoul.3
index a01a1d8db..32bd36dfc 100644
--- a/man3/strtoul.3
+++ b/man3/strtoul.3
@@ -124,6 +124,9 @@ .SH RETURN VALUE
 instead of
 .BR ULONG_MAX ).
 .SH ERRORS
+This function does not modify
+.I errno
+on success.
 .TP
 .B EINVAL
 (not in C99)
-- 
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).