public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
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 1/2] Revert "strtol.3: EXAMPLES: Simplify errno checking"
Date: Thu, 30 Nov 2023 16:29:31 +0100	[thread overview]
Message-ID: <20231130152910.322395-2-alx@kernel.org> (raw)

[-- 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 --]

             reply	other threads:[~2023-11-30 15:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-30 15:29 Alejandro Colomar [this message]
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

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-2-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).