UB status of snprintf on invalid ptr+size combination?
Stephan Bergmann
sbergman@redhat.com
Thu Mar 16 10:29:31 GMT 2023
On 15/03/2023 13:39, Vincent Lefevre wrote:
> No, it is not obvious. If the C standard does not say that this is
> the size of the array, then it does not have to be the size of the
> array. The C standard just says:
>
> Otherwise, output characters beyond the n-1st are discarded rather
> than being written to the array, and a null character is written at
> the end of the characters actually written into the array.
But in 7.1.4 "Use of library functions" the standard also says
> If a function argument is described as being an array, the pointer passed to the function shall
> have a value such that all address computations and accesses to objects (that would be valid if
> the pointer did point to the first element of such an array) are valid.
which could be construed as meaning that the n-1st array element must
always be accessible, even if a given invocation is known to always
generate less then n output characters.
More information about the Libc-alpha
mailing list