Skip to contentSkip to conversation

Revert "strtol.3: EXAMPLES: Simplify errno checking"

PATCH New Alejandro Colomar 1 participant 3 messages Nov 30, 2023 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore

Alejandro Colomar

author maintainer
#0 lore raw

PATCH 1/2 Revert "strtol.3: EXAMPLES: Simplify errno checking"

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.

---
 man3/strtol.3 | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
man3/strtol.3 +3 -2
index 01c658025..a5082a761 100644
@@ -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);
     }
Signature · 1 line
2.42.0
author maintainer
#1 lore raw

PATCH 2/2 strtol.3: EXAMPLES: Fix error checking

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 <[email protected]>
Cc Iker Pedrosa <[email protected]>
---
 man3/strtol.3 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
man3/strtol.3 +2 -1
index a5082a761..be8cc81d9 100644
@@ -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);
     }
Signature · 1 line
2.42.0
author maintainer
#2 lore raw

PATCH v2 1/1 strtol.3, strtoul.3: ERRORS: Clarify that these don't set errno on success

In reply to Alejandro Colomar

That's a guarantee made by POSIX.

Cc Florian Weimer <[email protected]>
Cc Iker Pedrosa <[email protected]>
Cc Michael Kerrisk <[email protected]>
---
 man3/strtol.3  | 3 +++
 man3/strtoul.3 | 3 +++
 2 files changed, 6 insertions(+)
man3/strtol.3 +3 -0
index 01c658025..1f1f98216 100644
@@ -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)
man3/strtoul.3 +3 -0
index a01a1d8db..32bd36dfc 100644
@@ -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)
Signature · 1 line
2.42.0