From: Alejandro Colomar <alx@kernel.org>
To: bug-gnulib@gnu.org
Cc: "Alejandro Colomar" <alx@kernel.org>,
"Paul Eggert" <eggert@cs.ucla.edu>,
"Bruno Haible" <bruno@clisp.org>,
"Đoàn Trần Công Danh" <congdanhqx@gmail.com>,
"Eli Schwartz" <eschwartz93@gmail.com>,
"Sam James" <sam@gentoo.org>, "Serge Hallyn" <serge@hallyn.com>,
"Iker Pedrosa" <ipedrosa@redhat.com>,
"Michael Vetter" <jubalh@iodoru.org>,
liba2i@lists.linux.dev
Subject: [PATCH v1 2/2] xstrtol: Add defensive check against undocumented errors
Date: Fri, 19 Jul 2024 14:53:39 +0200 [thread overview]
Message-ID: <20240719125312.15885-4-alx@kernel.org> (raw)
In-Reply-To: <20240719125312.15885-2-alx@kernel.org>
[-- Attachment #1: Type: text/plain, Size: 1499 bytes --]
strtod(3) calls malloc(3) in some systems. While strtol(3) doesn't,
let's be cautious and write code that would be safe under a theoretical
implementation of strtol(3) that could ENOMEM (and let's assume we don't
know what 'e' will look like after such an error).
Some attempt of defensive error handling had been added in 790855e18a1d,
but it was wrong, since it was assuming e!=nptr on such a case, which is
not a reasonable assumption.
Fixes: 790855e18a1d (2003-10-14, "Handle invalid suffixes and overflow independently, so that ...")
Cc: Paul Eggert <eggert@cs.ucla.edu>
Cc: Bruno Haible <bruno@clisp.org>
Cc: Đoàn Trần Công Danh <congdanhqx@gmail.com>
Cc: Eli Schwartz <eschwartz93@gmail.com>
Cc: Sam James <sam@gentoo.org>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Cc: Michael Vetter <jubalh@iodoru.org>
Cc: <liba2i@lists.linux.dev>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
lib/xstrtol.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/xstrtol.c b/lib/xstrtol.c
index 592673557f..faa7b9fd4c 100644
--- a/lib/xstrtol.c
+++ b/lib/xstrtol.c
@@ -94,7 +94,7 @@ __xstrtol (char const *nptr, char **endptr, int base,
if (endptr && e)
*endptr = e;
- if (e == nptr)
+ if (e == nptr && (errno == 0 || errno == EINVAL))
{
/* If there is no number but there is a valid suffix, assume the
number is 1. The string is invalid otherwise. */
--
2.45.2
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
prev parent reply other threads:[~2024-07-19 12:53 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-19 12:53 [PATCH v1 0/2] xstrtol() fixes Alejandro Colomar
2024-07-19 12:53 ` [PATCH v1 1/2] xstrtol: Correctly handle an invalid base Alejandro Colomar
2024-07-19 13:09 ` Alejandro Colomar
2024-07-19 13:10 ` Alejandro Colomar
2024-07-19 12:53 ` Alejandro Colomar [this message]
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=20240719125312.15885-4-alx@kernel.org \
--to=alx@kernel.org \
--cc=bruno@clisp.org \
--cc=bug-gnulib@gnu.org \
--cc=congdanhqx@gmail.com \
--cc=eggert@cs.ucla.edu \
--cc=eschwartz93@gmail.com \
--cc=ipedrosa@redhat.com \
--cc=jubalh@iodoru.org \
--cc=liba2i@lists.linux.dev \
--cc=sam@gentoo.org \
--cc=serge@hallyn.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