From: Alejandro Colomar <alx@kernel.org>
To: linux-man@vger.kernel.org
Cc: Alejandro Colomar <alx@kernel.org>,
libc-help@sourceware.org, ~hallyn/shadow@lists.sr.ht,
Michael Kerrisk <mtk.manpages@gmail.com>,
Florian Weimer <fweimer@redhat.com>,
Iker Pedrosa <ipedrosa@redhat.com>
Subject: [PATCH 2/2] strtol.3: EXAMPLES: Fix error checking
Date: Thu, 30 Nov 2023 16:30:26 +0100 [thread overview]
Message-ID: <20231130152910.322395-4-alx@kernel.org> (raw)
In-Reply-To: <20231130152910.322395-2-alx@kernel.org>
[-- 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 --]
next prev parent reply other threads:[~2023-11-30 15:30 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-30 15:29 [PATCH 1/2] Revert "strtol.3: EXAMPLES: Simplify errno checking" Alejandro Colomar
2023-11-30 15:30 ` Alejandro Colomar [this message]
2023-11-30 23:07 ` [PATCH v2] strtol.3, strtoul.3: ERRORS: Clarify that these don't set errno on success Alejandro Colomar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231130152910.322395-4-alx@kernel.org \
--to=alx@kernel.org \
--cc=fweimer@redhat.com \
--cc=ipedrosa@redhat.com \
--cc=libc-help@sourceware.org \
--cc=linux-man@vger.kernel.org \
--cc=mtk.manpages@gmail.com \
--cc=~hallyn/shadow@lists.sr.ht \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).