From: Alejandro Colomar <alx@kernel.org>
To: linux-man@vger.kernel.org
Cc: Alejandro Colomar <alx@kernel.org>,
Matthew House <mattlloydhouse@gmail.com>
Subject: [PATCH] strtol.3: Clarify that the base should be tested beforehand, if at all
Date: Wed, 6 Dec 2023 22:34:45 +0100 [thread overview]
Message-ID: <20231206213421.220065-2-alx@kernel.org> (raw)
[-- Attachment #1: Type: text/plain, Size: 2154 bytes --]
Normally, the base need not be tested, and the only interesting errno
value should be ERANGE.
If the base needs to be tested, it should be tested in a call that
would not otherwise fail. Otherwise, it would be easy to trigger
Undefined Behavior. Consider the following example:
errno = 0;
val = strtol("foo", &end, -42);
There's no portable way to know if the call failed due to the string or
the base.
Cc: Matthew House <mattlloydhouse@gmail.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
man3/strtol.3 | 28 +++++++++++++++++++++++++---
1 file changed, 25 insertions(+), 3 deletions(-)
diff --git a/man3/strtol.3 b/man3/strtol.3
index 1f1f98216..4b30b3269 100644
--- a/man3/strtol.3
+++ b/man3/strtol.3
@@ -184,8 +184,8 @@ .SH NOTES
.I errno
to 0 before the call,
and then determine if an error occurred by checking whether
-.I errno
-has a nonzero value after the call.
+.I errno == ERANGE
+after the call.
.P
According to POSIX.1,
in locales other than "C" and "POSIX",
@@ -206,6 +206,21 @@ .SH NOTES
.BR strtoll ()
or to
.BR strtol ().
+.SH CAVEATS
+If the
+.I base
+needs to be tested,
+it should be tested in a call where the string is known to succeed.
+Otherwise, it's impossible to portably differentiate the errors.
+.P
+.in +4n
+.EX
+errno = 0;
+strtol("0", NULL, base);
+if (errno == EINVAL)
+ goto unsupported_base;
+.EE
+.in
.SH EXAMPLES
The program shown below demonstrates the use of
.BR strtol ().
@@ -260,13 +275,20 @@ .SS Program source
\&
str = argv[1];
base = (argc > 2) ? atoi(argv[2]) : 0;
+\&
+ errno = 0; /* To distinguish success/failure after call */
+ strtol("0", NULL, base);
+ if (errno == EINVAL) {
+ fprintf(stderr, "The base is not supported\en");
+ exit(EXIT_FAILURE);
+ }
\&
errno = 0; /* To distinguish success/failure after call */
val = strtol(str, &endptr, base);
\&
/* Check for various possible errors. */
\&
- if (errno != 0) {
+ if (errno == ERANGE) {
perror("strtol");
exit(EXIT_FAILURE);
}
--
2.42.0
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
reply other threads:[~2023-12-06 21:34 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20231206213421.220065-2-alx@kernel.org \
--to=alx@kernel.org \
--cc=linux-man@vger.kernel.org \
--cc=mattlloydhouse@gmail.com \
/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