From: Matthew House <mattlloydhouse@gmail.com>
To: Alejandro Colomar <alx@kernel.org>
Cc: linux-man <linux-man@vger.kernel.org>,
Zack Weinberg <zack@owlfolio.org>,
Lee Griffiths <poddster@gmail.com>
Subject: Re: [PATCH] sscanf.3: Remove term 'deprecated', and expand BUGS
Date: Wed, 6 Dec 2023 15:45:19 -0500 [thread overview]
Message-ID: <20231206204522.756572-1-mattlloydhouse@gmail.com> (raw)
In-Reply-To: <ZXCjD5dP-jaUpeER@debian> <ZXDXBngCYG11NsMZ@debian>
On Wed, Dec 6, 2023 at 3:18 PM Alejandro Colomar <alx@kernel.org> wrote:
> Hi Matthew,
>
> On Wed, Dec 06, 2023 at 01:33:50PM -0500, Matthew House wrote:
> > I feel like this is rather overstating the difficulty. In practice, the
> > no-conversion condition is very commonly detected by checking whether
> > *endptr == nptr after the call. The usual idiom I see is something like:
> >
> > char *end;
> > errno = 0;
> > value = strtol(ptr, &end, 10);
> > if (end == ptr || *end != '\0' || errno == ERANGE)
>
> That test could trigger UB, if you passed an unsupported base. Of
> course, in this case you pass 10, but what if the base was a
> user-controlled variable? In such a case, nothing says what happens to
> 'end' (experimentally, I see it is not modified, so it would be left
> uninitialized); so dereferencing it, or even comparing it, would be UB.
>
> > goto err;
>
> Yeah, if you just don't care and want to handle all errors in the same
> way, and you know the base is supported, this is correct.
The practical answer is that the base is never ultimately a user-controlled
variable. Sometimes people define wrapper functions with a variable base,
but that base is still ultimately fixed by all its callers. If you disagree
with this, I challenge you to name a single example.
The theoretical answer is that you can just replace (errno == ERANGE) with
(errno != 0), or just (errno), if you still don't care about distinguishing
a base error. If you do care about distinguishing a base error, you can
just check its value directly, which, as I said, most people prefer over
trying to decode different funnily-named values of errno in my experience.
if (!(base == 0 || base >= 2 && base <= 36))
goto bad_base;
char *end;
errno = 0;
value = strtol(ptr, &end, base);
if (end == ptr)
goto not_a_number;
if (*end != '\0')
goto trailing_garbage;
if (errno == ERANGE)
goto overflow_error;
/* the last could also be, e.g., if (value < 0 || value > MAX_VALUE) */
Thank you,
Matthew House
next prev parent reply other threads:[~2023-12-06 20:45 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-06 14:52 [PATCH] sscanf.3: Remove term 'deprecated', and expand BUGS Alejandro Colomar
2023-12-06 16:36 ` Alejandro Colomar
2023-12-06 18:33 ` Matthew House
2023-12-06 20:17 ` Alejandro Colomar
2023-12-06 20:45 ` Matthew House [this message]
2023-12-06 20:54 ` Matthew House
2023-12-06 21:12 ` Alejandro Colomar
[not found] ` <CAKXok1GQvKi2HiBU89CSd+KF_dd9+mOMVhHrMKAVLLwcyJDN2g@mail.gmail.com>
2023-12-07 21:50 ` Fwd: " Lee Griffiths
2023-12-09 11:55 ` 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=20231206204522.756572-1-mattlloydhouse@gmail.com \
--to=mattlloydhouse@gmail.com \
--cc=alx@kernel.org \
--cc=linux-man@vger.kernel.org \
--cc=poddster@gmail.com \
--cc=zack@owlfolio.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