Wrong snprintf optimalization

Marek Polacek polacek@redhat.com
Mon Apr 23 13:46:00 GMT 2018


On Sat, Apr 21, 2018 at 11:47:52AM +0200, Dávid Bolvanský wrote:
> Hello,
> 
> #include <math.h>
> int main(void)
> {
> char buf[10];
> return snprintf(buf, 0, "string");
> }
> 
> GCC simplifies it to
> main:
> mov eax, 6
> ret
> 
> but 0 is correct I think.

No, 6 seems to be the correct value:
If the output was truncated due to this limit, then the return value
is the number of characters (excluding the terminating null byte) which would have been  written
to  the  final  string if enough space had been available.

	Marek



More information about the Gcc-bugs mailing list