UB status of snprintf on invalid ptr+size combination?

Andreas Schwab schwab@linux-m68k.org
Sun Mar 19 23:24:51 GMT 2023


On Mär 19 2023, Vincent Lefevre wrote:

> On 2023-03-18 16:01:07 +0100, Andreas Schwab wrote:
>> On Mär 18 2023, Vincent Lefevre wrote:
>> 
>> > However, I'm wondering whether such a change is intentional. BTW,
>> > this description is even wrong: this is certainly not equivalent!
>> > If the untruncated output is larger than n, then the call is UB
>> > with sprintf(), while the output is truncated with snprintf().
>> 
>> Which makes them equivalent in all situations where sprintf is defined,
>> so there is no discrepancy here.
>
> The conditions under which the function is defined or not are part of
> the equivalence.

No, it isn't.  Equivalent is not the same as identical.

> For instance, I would not say that memcpy and memmove are equivalent,
> even though they are equivalent when memcpy is defined.

But they are.  You can use either memcpy or memove in all situations
where memcpy is defined.  Outside of the domain of memcpy there is no
relation at all.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."


More information about the Libc-alpha mailing list