From: Bruno Haible <bruno@clisp.org>
To: Alejandro Colomar <alx.manpages@gmail.com>, linux-man@vger.kernel.org
Subject: strtoul pitfalls
Date: Fri, 21 Mar 2025 20:05:18 +0100 [thread overview]
Message-ID: <1818253.7aRn1RRit1@nimes> (raw)
Hi Alejandro,
reviewing the strtoul() calls in gnulib and gettext, I found several
places where the original coder had fallen into one of these two pitfalls.
I would find it good to mention both pitfalls more prominently in the
man-pages (the first one is mentioned as a NOTE, should better be called
a BUG).
* Leading whitespace and a sign are accepted by default.
Often this is not desired.
To avoid this, the caller should pre-parse the initial portion
of the string, up to the first digit.
* If values > MAX should lead to a diagnostic, they arise in two
circumstances:
- errno = ERANGE,
- errno = 0, end == str, but value > MAX.
It is not obvious how to arrange the code, so as to get the same
diagnostic, without code duplication.
Bruno
[1] https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=commitdiff;h=008ff109def9bdfdd04a12355d8f4a835045ae41
[2] https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=commitdiff;h=3df6da258c338e4fd165a2850bb8d4b0f39b6e9b
[3] https://git.savannah.gnu.org/gitweb/?p=gettext.git;a=commitdiff;h=b8ab4f2bef0ff18cf1ae749c1c81a359a40d0450
next reply other threads:[~2025-03-21 19:08 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-21 19:05 Bruno Haible [this message]
2025-03-21 20:02 ` strtoul pitfalls 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=1818253.7aRn1RRit1@nimes \
--to=bruno@clisp.org \
--cc=alx.manpages@gmail.com \
--cc=linux-man@vger.kernel.org \
/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