Revert "strtol.3: EXAMPLES: Simplify errno checking"
Alejandro Colomar
maintainerPATCH 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
@@ -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
Alejandro Colomar
maintainerPATCH 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.
--- man3/strtol.3 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
man3/strtol.3 +2 -1
@@ -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
Alejandro Colomar
maintainerPATCH v2 1/1 strtol.3, strtoul.3: ERRORS: Clarify that these don't set errno on success
In reply to Alejandro ColomarThat's a guarantee made by POSIX.
--- man3/strtol.3 | 3 +++ man3/strtoul.3 | 3 +++ 2 files changed, 6 insertions(+)
man3/strtol.3 +3 -0
@@ -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
@@ -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