UB status of snprintf on invalid ptr+size combination?

Vincent Lefevre vincent@vinc17.net
Mon Mar 20 15:09:29 GMT 2023


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)


More information about the Libc-alpha mailing list