From: Vincent Lefevre <vincent@vinc17.net>
To: libc-alpha@sourceware.org
Subject: Re: UB status of snprintf on invalid ptr+size combination?
Date: Mon, 20 Mar 2023 16:09:29 +0100 [thread overview]
Message-ID: <20230320150929.GA283644@cventin.lip.ens-lyon.fr> (raw)
In-Reply-To: <9d7ca3d8-6998-e741-b669-03ef42bc99f1@gmail.com>
On 2023-03-19 10:45:59 -0400, manfred via Libc-alpha wrote:
> All of that said, back to the OP case I would not pass INT_MAX to snprintf.
> If I have a situation wherein I know that the buffer is large enough, but I
> don't know its exact size, I'd use sprintf and be done with it. (I'm sure
> that the actual code is more elaborate than this, but still)
Here's another example where the support of snprintf with a large n
argument (larger than the buffer size) may be used:
In GNU MPFR, for our function mpfr_snprintf, we have assumed a
behavior analogue to the ISO C behavior. In particular, we use
that in our tests in order to check that large values of n are
correctly handled. This allowed us to trigger/detect a bug in
the choice of the integer types in our implementation:
The test:
https://gitlab.inria.fr/mpfr/mpfr/-/commit/67a75bfe41d3a7f95367ee9e62bd7dfc73e5b395
The bug fix (replacing an int by a size_t in a variable declaration):
https://gitlab.inria.fr/mpfr/mpfr/-/commit/6b8cf3e2bdc285027627281cac230ed932c1b73f
Note that the MPFR code currently does not use snprintf or any
similar function, so that there should be no issues with this
test if snprintf does not support n > buffer size, it but may
use such a function in the future. (Indeed, MPFR currently uses
gmp_vasprintf and gmp_asprintf, thus generating a full output
before truncating it as needed, so that this is potentially
very inefficient and could unnecessarily exhaust the memory
and/or crash.)
--
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)
next prev parent reply other threads:[~2023-03-20 15:09 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-14 19:47 Simon Chopin
2023-03-14 21:39 ` Paul Eggert
2023-03-15 9:22 ` Andreas Schwab
2023-03-15 15:54 ` Siddhesh Poyarekar
2023-03-15 18:34 ` Michael Hudson-Doyle
2023-03-19 14:45 ` manfred
2023-03-19 23:07 ` Vincent Lefevre
2023-03-20 12:05 ` Siddhesh Poyarekar
2023-03-20 12:17 ` Alejandro Colomar
2023-03-20 12:29 ` Siddhesh Poyarekar
2023-03-20 13:36 ` Vincent Lefevre
2023-03-20 13:50 ` Vincent Lefevre
2023-03-20 16:56 ` Siddhesh Poyarekar
2023-03-20 17:36 ` Vincent Lefevre
2023-03-20 15:09 ` Vincent Lefevre [this message]
2023-03-20 16:15 ` Alejandro Colomar
2023-03-20 16:33 ` Vincent Lefevre
2023-03-20 17:00 ` Vincent Lefevre
2023-03-20 17:31 ` Siddhesh Poyarekar
2023-03-20 17:45 ` Vincent Lefevre
2023-03-15 12:39 ` Vincent Lefevre
2023-03-16 10:29 ` Stephan Bergmann
2023-03-18 2:07 ` Vincent Lefevre
2023-03-18 2:30 ` Alejandro Colomar
2023-03-18 10:58 ` Vincent Lefevre
2023-03-18 15:01 ` Andreas Schwab
2023-03-19 22:48 ` Vincent Lefevre
2023-03-19 23:24 ` Andreas Schwab
2023-03-20 4:10 ` Vincent Lefevre
2023-03-20 9:19 ` Andreas Schwab
2023-03-20 10:42 ` Vincent Lefevre
2023-03-20 10:44 ` Andreas Schwab
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=20230320150929.GA283644@cventin.lip.ens-lyon.fr \
--to=vincent@vinc17.net \
--cc=libc-alpha@sourceware.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).