Minor improvements:

o Explain snprintf's return value better.
o Document snprintf, et al, were defined in C-99
o Warn against %n.
This commit is contained in:
Warner Losh 2001-06-05 04:22:30 +00:00
parent 1239674238
commit 890d5b436d
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=77744

View file

@ -114,8 +114,7 @@ string that specifies how subsequent arguments
.Xr stdarg 3 ) .Xr stdarg 3 )
are converted for output. are converted for output.
.Pp .Pp
These functions return These functions return the number of characters printed
the number of characters printed
(not including the trailing (not including the trailing
.Ql \e0 .Ql \e0
used to end output to strings), used to end output to strings),
@ -151,17 +150,16 @@ to be a NULL pointer.
.Fn Snprintf .Fn Snprintf
and and
.Fn vsnprintf .Fn vsnprintf
will write at most return the number of characters
.Fa size Ns \-1 that would have been written had
of the characters printed into the output string .Fa size
(the been sufficiently large, not counting the terminating
.Fa size Ns 'th .Ql \e0
character then gets the terminating character, or a negative value if an encoding error occurred.
.Ql \e0 ) ; Thus, the null-terminated output has been completely written if and only if
if the return value is greater than or equal to the the returned value is nonnegative and less than
.Fa size .Fa size .
argument, the string was too short The output is always null-terminated.
and some of the printed characters were discarded.
.Pp .Pp
.Fn Sprintf .Fn Sprintf
and and
@ -623,7 +621,15 @@ and
.Fn vsprintf .Fn vsprintf
functions functions
conform to conform to
.St -isoC . .St -ansiC
and
.St -isoC-99 .
The
.Fn snprintf
and
.Fn vsnprintf
functions conform to
.St -isoC-99 .
.Sh HISTORY .Sh HISTORY
The functions The functions
.Fn asprintf .Fn asprintf
@ -674,7 +680,15 @@ this is often hard to assure.
For safety, programmers should use the For safety, programmers should use the
.Fn snprintf .Fn snprintf
interface instead. interface instead.
Unfortunately, this interface is not portable. Unfortunately, this interface was only defined in
.St -isoC-99 .
.Pp
.Cm %n
can be used to write arbitrary data to the stack.
Programmers are therefore strongly advised to never pass untrusted strings
as the
.Fa format
argument.
.Pp .Pp
Never pass a string with user-supplied data as a format without using Never pass a string with user-supplied data as a format without using
.Ql %s . .Ql %s .