GCC 12.1 Released
GCC 12.1 Released
Posted May 11, 2022 20:44 UTC (Wed) by wtarreau (subscriber, #51152)In reply to: GCC 12.1 Released by geert
Parent article: GCC 12.1 Released
https://pubs.opengroup.org/onlinepubs/7908799/xsh/snprint...
RETURN VALUE
Upon successful completion, these functions return the number of bytes
transmitted excluding the terminating null in the case of sprintf() or snprintf()
or a negative value if an output error was encountered.
On Linux+glibc:
The functions snprintf() and vsnprintf() do not write more than size
bytes (including the terminating null byte ('\0')). 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.
That's what most modern systems do, allowing you to realloc() the area and try
again. Some do not support being passed size zero, others do.
snprintf() is one of the most important and least portable functions when it comes
to good security practices. There's also %z (size_t) that's not much portable, and
"%.*s" that often does fun things like shifting all args by one since %.* is not
understood as consuming an extra argument, so usually you segfault by trying to
print the string from a pointer that's in fact its max length.
