snprintf() confusion
Posted Feb 5, 2004 8:43 UTC (Thu) by
ThePythonicCow (subscriber, #11308)
In reply to:
snprintf() confusion by IkeTo
Parent article:
snprintf() confusion
Historically, there have been two variants of snprintf,
with the ISO C99 standard, taken from BSD, of returning
the length of the entire formatted string, even if it
didn't entirely fit.
Earlier versions of snprintf only returned a count of how
many non-nul characters went into the buffer.
This earlier return convention was actually "obvious" at
the time -- no one on those slow, old machines would expect
a C library routine to be wasting its CPU cycles calculating
the length of some formatted output that it wasn't going to
output.
And my impression is that most of us old timers are surprised
by the new convention.
This new convention is also not worth much in most kernel code,
which doesn't do anything with the extra information anyway.
If the intended output doesn't fit (it's usually a console
print or something) then tough - it gets truncated. Not usually
worth the bother to try to expand the buffer size (and you have
to be careful, not to open the kernel up to a Denial of Service
attack, if the user can somehow persuade it to attempt to print
something that's surprisingly long.
Still, Linus has clearly chosen the new ISO C99 standard (someone
backed the lib/vsprintf.c code off to the "old standard", and
Linus restored the C99 conventions.
(
Log in to post comments)