]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/stdio/printf.3
Add new "SECURITY CONSIDERATIONS" sections.
[FreeBSD/FreeBSD.git] / lib / libc / stdio / printf.3
1 .\" Copyright (c) 1990, 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" This code is derived from software contributed to Berkeley by
5 .\" Chris Torek and the American National Standards Committee X3,
6 .\" on Information Processing Systems.
7 .\"
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
10 .\" are met:
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\"    notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\"    notice, this list of conditions and the following disclaimer in the
15 .\"    documentation and/or other materials provided with the distribution.
16 .\" 3. All advertising materials mentioning features or use of this software
17 .\"    must display the following acknowledgement:
18 .\"     This product includes software developed by the University of
19 .\"     California, Berkeley and its contributors.
20 .\" 4. Neither the name of the University nor the names of its contributors
21 .\"    may be used to endorse or promote products derived from this software
22 .\"    without specific prior written permission.
23 .\"
24 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" SUCH DAMAGE.
35 .\"
36 .\"     @(#)printf.3    8.1 (Berkeley) 6/4/93
37 .\" $FreeBSD$
38 .\"
39 .Dd November 8, 2001
40 .Dt PRINTF 3
41 .Os
42 .Sh NAME
43 .Nm printf , fprintf , sprintf , snprintf , asprintf ,
44 .Nm vprintf , vfprintf, vsprintf , vsnprintf , vasprintf
45 .Nd formatted output conversion
46 .Sh LIBRARY
47 .Lb libc
48 .Sh SYNOPSIS
49 .In stdio.h
50 .Ft int
51 .Fn printf "const char *format" ...
52 .Ft int
53 .Fn fprintf "FILE *stream" "const char *format" ...
54 .Ft int
55 .Fn sprintf "char *str" "const char *format" ...
56 .Ft int
57 .Fn snprintf "char *str" "size_t size" "const char *format" ...
58 .Ft int
59 .Fn asprintf "char **ret" "const char *format" ...
60 .In stdarg.h
61 .Ft int
62 .Fn vprintf "const char *format" "va_list ap"
63 .Ft int
64 .Fn vfprintf "FILE *stream" "const char *format" "va_list ap"
65 .Ft int
66 .Fn vsprintf "char *str" "const char *format" "va_list ap"
67 .Ft int
68 .Fn vsnprintf "char *str" "size_t size" "const char *format" "va_list ap"
69 .Ft int
70 .Fn vasprintf "char **ret" "const char *format" "va_list ap"
71 .Sh DESCRIPTION
72 The
73 .Fn printf
74 family of functions produces output according to a
75 .Fa format
76 as described below.
77 .Fn Printf
78 and
79 .Fn vprintf
80 write output to
81 .Pa stdout ,
82 the standard output stream;
83 .Fn fprintf
84 and
85 .Fn vfprintf
86 write output to the given output
87 .Fa stream ;
88 .Fn sprintf ,
89 .Fn snprintf ,
90 .Fn vsprintf ,
91 and
92 .Fn vsnprintf
93 write to the character string
94 .Fa str ;
95 and
96 .Fn asprintf
97 and
98 .Fn vasprintf
99 dynamically allocate a new string with
100 .Xr malloc 3 .
101 .Pp
102 These functions write the output under the control of a
103 .Fa format
104 string that specifies how subsequent arguments
105 (or arguments accessed via the variable-length argument facilities of
106 .Xr stdarg 3 )
107 are converted for output.
108 .Pp
109 These functions return the number of characters printed
110 (not including the trailing
111 .Ql \e0
112 used to end output to strings),
113 except for
114 .Fn snprintf
115 and
116 .Fn vsnprintf ,
117 which return the number of characters that would have been printed if the
118 .Fa size
119 were unlimited
120 (again, not including the final
121 .Ql \e0 ) .
122 .Pp
123 .Fn Asprintf
124 and
125 .Fn vasprintf
126 set
127 .Fa *ret
128 to be a pointer to a buffer sufficiently large to hold the formatted string.
129 This pointer should be passed to
130 .Xr free 3
131 to release the allocated storage when it is no longer needed.
132 If sufficient space cannot be allocated,
133 .Fn asprintf
134 and
135 .Fn vasprintf
136 will return \-1 and set
137 .Fa ret
138 to be a
139 .Dv NULL
140 pointer.
141 .Pp
142 .Fn Snprintf
143 and
144 .Fn vsnprintf
145 will write at most
146 .Fa size Ns \-1
147 of the characters printed into the output string
148 (the
149 .Fa size Ns 'th
150 character then gets the terminating
151 .Ql \e0 ) ;
152 if the return value is greater than or equal to the
153 .Fa size
154 argument, the string was too short
155 and some of the printed characters were discarded.
156 The output is always null-terminated.
157 .Pp
158 .Fn Sprintf
159 and
160 .Fn vsprintf
161 effectively assume an infinite
162 .Fa size .
163 .Pp
164 The format string is composed of zero or more directives:
165 ordinary
166 .\" multibyte
167 characters (not
168 .Cm % ) ,
169 which are copied unchanged to the output stream;
170 and conversion specifications, each of which results
171 in fetching zero or more subsequent arguments.
172 Each conversion specification is introduced by
173 the
174 .Cm %
175 character.
176 The arguments must correspond properly (after type promotion)
177 with the conversion specifier.
178 After the
179 .Cm % ,
180 the following appear in sequence:
181 .Bl -bullet
182 .It
183 An optional field, consisting of a decimal digit string followed by a
184 .Cm $ ,
185 specifying the next argument to access.
186 If this field is not provided, the argument following the last
187 argument accessed will be used.
188 Arguments are numbered starting at
189 .Cm 1 .
190 If unaccessed arguments in the format string are interspersed with ones that
191 are accessed the results will be indeterminate.
192 .It
193 Zero or more of the following flags:
194 .Bl -tag -width ".So \  Sc (space)"
195 .It Sq Cm #
196 The value should be converted to an
197 .Dq alternate form .
198 For
199 .Cm c , d , i , n , p , s ,
200 and
201 .Cm u
202 conversions, this option has no effect.
203 For
204 .Cm o
205 conversions, the precision of the number is increased to force the first
206 character of the output string to a zero (except if a zero value is printed
207 with an explicit precision of zero).
208 For
209 .Cm x
210 and
211 .Cm X
212 conversions, a non-zero result has the string
213 .Ql 0x
214 (or
215 .Ql 0X
216 for
217 .Cm X
218 conversions) prepended to it.
219 For
220 .Cm a , A , e , E , f , F , g ,
221 and
222 .Cm G
223 conversions, the result will always contain a decimal point, even if no
224 digits follow it (normally, a decimal point appears in the results of
225 those conversions only if a digit follows).
226 For
227 .Cm g
228 and
229 .Cm G
230 conversions, trailing zeros are not removed from the result as they
231 would otherwise be.
232 .It So Cm 0 Sc (zero)
233 Zero padding.
234 For all conversions except
235 .Cm n ,
236 the converted value is padded on the left with zeros rather than blanks.
237 If a precision is given with a numeric conversion
238 .Cm ( d , i , o , u , i , x ,
239 and
240 .Cm X ) ,
241 the
242 .Cm 0
243 flag is ignored.
244 .It Sq Cm \-
245 A negative field width flag;
246 the converted value is to be left adjusted on the field boundary.
247 Except for
248 .Cm n
249 conversions, the converted value is padded on the right with blanks,
250 rather than on the left with blanks or zeros.
251 A
252 .Cm \-
253 overrides a
254 .Cm 0
255 if both are given.
256 .It So "\ " Sc (space)
257 A blank should be left before a positive number
258 produced by a signed conversion
259 .Cm ( a , A , d , e , E , f , F , g , G ,
260 or
261 .Cm i ) .
262 .It Sq Cm +
263 A sign must always be placed before a
264 number produced by a signed conversion.
265 A
266 .Cm +
267 overrides a space if both are used.
268 .It Sq Cm '
269 Decimal conversions
270 .Cm ( d , u ,
271 or
272 .Cm i )
273 or the integral portion of a floating point conversion
274 .Cm ( f
275 or
276 .Cm F )
277 should be grouped and separated by thousands using
278 the non-monetary seperator returned by
279 .Xr localeconv 3 .
280 .El
281 .It
282 An optional decimal digit string specifying a minimum field width.
283 If the converted value has fewer characters than the field width, it will
284 be padded with spaces on the left (or right, if the left-adjustment
285 flag has been given) to fill out
286 the field width.
287 .It
288 An optional precision, in the form of a period
289 .Cm \&.
290 followed by an
291 optional digit string.
292 If the digit string is omitted, the precision is taken as zero.
293 This gives the minimum number of digits to appear for
294 .Cm d , i , o , u , x ,
295 and
296 .Cm X
297 conversions, the number of digits to appear after the decimal-point for
298 .Cm a , A , e , E , f ,
299 and
300 .Cm F
301 conversions, the maximum number of significant digits for
302 .Cm g
303 and
304 .Cm G
305 conversions, or the maximum number of characters to be printed from a
306 string for
307 .Cm s
308 conversions.
309 .It
310 An optional length modifier, that specifies the size of the argument.
311 The following length modifiers are valid for the
312 .Cm d , i , n , o , u , x ,
313 or
314 .Cm X
315 conversion:
316 .Bl -column ".Cm q Em (deprecated)" ".Vt signed char" ".Vt unsigned long long" ".Vt long long *"
317 .It Sy Modifier Ta Cm d , i Ta Cm o , u , x , X Ta Cm n
318 .It Cm hh Ta Vt "signed char" Ta Vt "unsigned char" Ta Vt "signed char *"
319 .It Cm h Ta Vt short Ta Vt "unsigned short" Ta Vt "short *"
320 .It Cm l No (ell) Ta Vt long Ta Vt "unsigned long" Ta Vt "long *"
321 .It Cm ll No (ell ell) Ta Vt "long long" Ta Vt "unsigned long long" Ta Vt "long long *"
322 .It Cm j Ta Vt intmax_t Ta Vt uintmax_t Ta Vt "intmax_t *"
323 .It Cm t Ta Vt ptrdiff_t Ta (see note) Ta Vt "ptrdiff_t *"
324 .It Cm z Ta (see note) Ta Vt size_t Ta (see note)
325 .It Cm q Em (deprecated) Ta Vt quad_t Ta Vt u_quad_t Ta Vt "quad_t *"
326 .El
327 .Pp
328 Note:
329 the
330 .Cm t
331 modifier, when applied to a
332 .Cm o , u , x ,
333 or
334 .Cm X
335 conversion, indicates that the argument is of an unsigned type
336 equivalent in size to a
337 .Vt ptrdiff_t .
338 The
339 .Cm z
340 modifier, when applied to a
341 .Cm d
342 or
343 .Cm i
344 conversion, indicates that the argument is of a signed type equivalent in
345 size to a
346 .Vt size_t .
347 Similarly, when applied to an
348 .Cm n
349 conversion, it indicates that the argument is a pointer to a signed type
350 equivalent in size to a
351 .Vt size_t .
352 .Pp
353 The following length modifier is valid for the
354 .Cm a , A , e , E , f , F , g ,
355 or
356 .Cm G
357 conversion:
358 .Bl -column ".Sy Modifier" ".Cm a , A , e , E , f , F , g , G"
359 .It Sy Modifier Ta Cm a , A , e , E , f , F , g , G
360 .It Cm L Ta Vt "long double"
361 .El
362 .Pp
363 The following length modifier is valid for the
364 .Cm c
365 or
366 .Cm s
367 conversion:
368 .Bl -column ".Sy Modifier" ".Vt wint_t" ".Vt wchar_t *"
369 .It Sy Modifier Ta Cm c Ta Cm s
370 .It Cm l No (ell) Ta Vt wint_t Ta Vt "wchar_t *"
371 .El
372 .It
373 A character that specifies the type of conversion to be applied.
374 .El
375 .Pp
376 A field width or precision, or both, may be indicated by
377 an asterisk
378 .Ql *
379 or an asterisk followed by one or more decimal digits and a
380 .Ql $
381 instead of a
382 digit string.
383 In this case, an
384 .Vt int
385 argument supplies the field width or precision.
386 A negative field width is treated as a left adjustment flag followed by a
387 positive field width; a negative precision is treated as though it were
388 missing.
389 If a single format directive mixes positional
390 .Pq Li nn$
391 and non-positional arguments, the results are undefined.
392 .Pp
393 The conversion specifiers and their meanings are:
394 .Bl -tag -width ".Cm diouxX"
395 .It Cm diouxX
396 The
397 .Vt int
398 (or appropriate variant) argument is converted to signed decimal
399 .Cm ( d
400 and
401 .Cm i ) ,
402 unsigned octal
403 .Pq Cm o ,
404 unsigned decimal
405 .Pq Cm u ,
406 or unsigned hexadecimal
407 .Cm ( x
408 and
409 .Cm X )
410 notation.
411 The letters
412 .Dq Li abcdef
413 are used for
414 .Cm x
415 conversions; the letters
416 .Dq Li ABCDEF
417 are used for
418 .Cm X
419 conversions.
420 The precision, if any, gives the minimum number of digits that must
421 appear; if the converted value requires fewer digits, it is padded on
422 the left with zeros.
423 .It Cm DOU
424 The
425 .Vt "long int"
426 argument is converted to signed decimal, unsigned octal, or unsigned
427 decimal, as if the format had been
428 .Cm ld , lo ,
429 or
430 .Cm lu
431 respectively.
432 These conversion characters are deprecated, and will eventually disappear.
433 .It Cm eE
434 The
435 .Vt double
436 argument is rounded and converted in the style
437 .Sm off
438 .Oo \- Oc Ar d Li \&. Ar ddd Li e \\*[Pm] Ar dd
439 .Sm on
440 where there is one digit before the
441 decimal-point character
442 and the number of digits after it is equal to the precision;
443 if the precision is missing,
444 it is taken as 6; if the precision is
445 zero, no decimal-point character appears.
446 An
447 .Cm E
448 conversion uses the letter
449 .Ql E
450 (rather than
451 .Ql e )
452 to introduce the exponent.
453 The exponent always contains at least two digits; if the value is zero,
454 the exponent is 00.
455 .Pp
456 For
457 .Cm a , A , e , E , f , F , g ,
458 and
459 .Cm G
460 conversions, positive and negative infinity are represented as
461 .Li inf
462 and
463 .Li -inf
464 respectively when using the lowercase conversion character, and
465 .Li INF
466 and
467 .Li -INF
468 respectively when using the uppercase conversion character.
469 Similarly, NaN is represented as
470 .Li nan
471 when using the lowercase conversion, and
472 .Li NAN
473 when using the uppercase conversion.
474 .It Cm fF
475 The
476 .Vt double
477 argument is rounded and converted to decimal notation in the style
478 .Sm off
479 .Oo \- Oc Ar ddd Li \&. Ar ddd ,
480 .Sm on
481 where the number of digits after the decimal-point character
482 is equal to the precision specification.
483 If the precision is missing, it is taken as 6; if the precision is
484 explicitly zero, no decimal-point character appears.
485 If a decimal point appears, at least one digit appears before it.
486 .It Cm gG
487 The
488 .Vt double
489 argument is converted in style
490 .Cm f
491 or
492 .Cm e
493 (or
494 .Cm F
495 or
496 .Cm E
497 for
498 .Cm G
499 conversions).
500 The precision specifies the number of significant digits.
501 If the precision is missing, 6 digits are given; if the precision is zero,
502 it is treated as 1.
503 Style
504 .Cm e
505 is used if the exponent from its conversion is less than \-4 or greater than
506 or equal to the precision.
507 Trailing zeros are removed from the fractional part of the result; a
508 decimal point appears only if it is followed by at least one digit.
509 .It Cm aA
510 The
511 .Vt double
512 argument is converted to hexadecimal notation in the style
513 .Sm off
514 .Oo \- Oc Li 0x Ar h Li \&. Ar hhhp Oo \\*[Pm] Oc Ar d ,
515 .Sm on
516 where the number of digits after the hexadecimal-point character
517 is equal to the precision specification.
518 If the precision is missing, it is taken as enough to exactly
519 represent the floating-point number; if the precision is
520 explicitly zero, no hexadecimal-point character appears.
521 This is an exact coversion of the mantissa+exponent internal
522 floating point representation; the
523 .Sm off
524 .Oo \- Oc Li 0x Ar h Li \&. Ar hhh
525 .Sm on
526 portion represents exactly the mantissa; only denormalized
527 mantissas have a zero value to the left of the hexadecimal
528 point.
529 The
530 .Cm p
531 is a literal character
532 .Ql p ;
533 the exponent is preceded by a positive or negative sign
534 and is represented in decimal, using only enough characters
535 to represent the exponent.
536 The
537 .Cm A
538 conversion uses the prefix
539 .Dq Li 0X
540 (rather than
541 .Dq Li 0x ) ,
542 the letters
543 .Dq Li ABCDEF
544 (rather than
545 .Dq Li abcdef )
546 to represent the hex digits, and the letter
547 .Ql P
548 (rather than
549 .Ql p )
550 to seperate the mantissa and exponent.
551 .It Cm C
552 Treated as
553 .Cm c
554 with the
555 .Cm l
556 (ell) modifier.
557 .It Cm c
558 The
559 .Vt int
560 argument is converted to an
561 .Vt "unsigned char" ,
562 and the resulting character is written.
563 .Pp
564 If the
565 .Cm l
566 (ell) modifier is used, the
567 .Vt wint_t
568 argument shall be converted to a
569 .Vt wchar_t ,
570 and the (potentially multi-byte) sequence representing the
571 single wide character is written, including any shift sequences.
572 If a shift sequence is used, the shift state is also restored
573 to the original state after the character.
574 .It Cm S
575 Treated as
576 .Cm s
577 with the
578 .Cm l
579 (ell) modifier.
580 .It Cm s
581 The
582 .Vt "char *"
583 argument is expected to be a pointer to an array of character type (pointer
584 to a string).
585 Characters from the array are written up to (but not including)
586 a terminating
587 .Dv NUL
588 character;
589 if a precision is specified, no more than the number specified are
590 written.
591 If a precision is given, no null character
592 need be present; if the precision is not specified, or is greater than
593 the size of the array, the array must contain a terminating
594 .Dv NUL
595 character.
596 .Pp
597 If the
598 .Cm l
599 (ell) modifier is used, the
600 .Vt "wchar_t *"
601 argument is expected to be a pointer to an array of wide characters
602 (pointer to a wide string).
603 For each wide character in the string, the (potentially multi-byte)
604 sequence representing the
605 wide character is written, including any shift sequences.
606 If any shift sequence is used, the shift state is also restored
607 to the original state after the string.
608 Wide characters from the array are written up to (but not including)
609 a terminating wide
610 .Dv NUL
611 character;
612 if a precision is specified, no more than the number of bytes specified are
613 written (including shift sequences).
614 Partial characters are never written.
615 If a precision is given, no null character
616 need be present; if the precision is not specified, or is greater than
617 the number of bytes required to render the multibyte representation of
618 the string, the array must contain a terminating wide
619 .Dv NUL
620 character.
621 .It Cm p
622 The
623 .Vt "void *"
624 pointer argument is printed in hexadecimal (as if by
625 .Ql %#x
626 or
627 .Ql %#lx ) .
628 .It Cm n
629 The number of characters written so far is stored into the
630 integer indicated by the
631 .Vt "int *"
632 (or variant) pointer argument.
633 No argument is converted.
634 .It Cm %
635 A
636 .Ql %
637 is written.
638 No argument is converted.
639 The complete conversion specification
640 is
641 .Ql %% .
642 .El
643 .Pp
644 The decimal point
645 character is defined in the program's locale (category
646 .Dv LC_NUMERIC ) .
647 .Pp
648 In no case does a non-existent or small field width cause truncation of
649 a numeric field; if the result of a conversion is wider than the field
650 width, the
651 field is expanded to contain the conversion result.
652 .Sh EXAMPLES
653 To print a date and time in the form
654 .Dq Li "Sunday, July 3, 10:02" ,
655 where
656 .Fa weekday
657 and
658 .Fa month
659 are pointers to strings:
660 .Bd -literal -offset indent
661 #include <stdio.h>
662 fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
663         weekday, month, day, hour, min);
664 .Ed
665 .Pp
666 To print \*(Pi
667 to five decimal places:
668 .Bd -literal -offset indent
669 #include <math.h>
670 #include <stdio.h>
671 fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
672 .Ed
673 .Pp
674 To allocate a 128 byte string and print into it:
675 .Bd -literal -offset indent
676 #include <stdio.h>
677 #include <stdlib.h>
678 #include <stdarg.h>
679 char *newfmt(const char *fmt, ...)
680 {
681                 char *p;
682                 va_list ap;
683                 if ((p = malloc(128)) == NULL)
684                         return (NULL);
685                 va_start(ap, fmt);
686                 (void) vsnprintf(p, 128, fmt, ap);
687                 va_end(ap);
688                 return (p);
689 }
690 .Ed
691 .Sh SECURITY CONSIDERATIONS
692 The
693 .Fn sprintf
694 and
695 .Fn vsprintf
696 functions are easily misused in a manner which enables malicious users
697 to arbitrarily change a running program's functionality through
698 a buffer overflow attack.
699 (See
700 the FSA
701 and
702 .Sx EXAMPLES . )
703 .Sh SEE ALSO
704 .Xr printf 1 ,
705 .Xr scanf 3 ,
706 .Xr setlocale 3
707 .Rs
708 .%T "The FreeBSD Security Architecture"
709 .%J "/usr/share/doc/{to be determined}"
710 .Re
711 .Sh STANDARDS
712 The
713 .Fn fprintf ,
714 .Fn printf ,
715 .Fn sprintf ,
716 .Fn vprintf ,
717 .Fn vfprintf ,
718 and
719 .Fn vsprintf
720 functions
721 conform to
722 .St -ansiC
723 and
724 .St -isoC-99 .
725 The
726 .Fn snprintf
727 and
728 .Fn vsnprintf
729 functions conform to
730 .St -isoC-99 .
731 .Sh HISTORY
732 The functions
733 .Fn asprintf
734 and
735 .Fn vasprintf
736 first appeared in the
737 .Tn GNU C
738 library.
739 These were implemented by
740 .An Peter Wemm Aq peter@FreeBSD.org
741 in
742 .Fx 2.2 ,
743 but were later replaced with a different implementation
744 from
745 .An Todd C. Miller Aq Todd.Miller@courtesan.com
746 for
747 .Ox 2.3 .
748 .Sh BUGS
749 The conversion formats
750 .Cm \&%D , \&%O ,
751 and
752 .Cm %U
753 are not standard and
754 are provided only for backward compatibility.
755 The effect of padding the
756 .Cm %p
757 format with zeros (either by the
758 .Cm 0
759 flag or by specifying a precision), and the benign effect (i.e., none)
760 of the
761 .Cm #
762 flag on
763 .Cm %n
764 and
765 .Cm %p
766 conversions, as well as other
767 nonsensical combinations such as
768 .Cm %Ld ,
769 are not standard; such combinations
770 should be avoided.
771 .Pp
772 Because
773 .Fn sprintf
774 and
775 .Fn vsprintf
776 assume an infinitely long string,
777 callers must be careful not to overflow the actual space;
778 this is often hard to assure.
779 For safety, programmers should use the
780 .Fn snprintf
781 interface instead.
782 Unfortunately, this interface was only defined in
783 .St -isoC-99 .
784 .Pp
785 .Cm %n
786 can be used to write arbitrary data to the stack.
787 Programmers are therefore strongly advised to never pass untrusted strings
788 as the
789 .Fa format
790 argument.
791 .Pp
792 Never pass a string with user-supplied data as a format without using
793 .Ql %s .
794 An attacker can put format specifiers in the string to mangle your stack,
795 leading to a possible security hole.
796 This holds true even if the string was built using a function like
797 .Fn snprintf ,
798 as the resulting string may still contain user-supplied conversion specifiers
799 for later interpolation by
800 .Fn printf .
801 .Pp
802 Always use the proper secure idiom:
803 .Pp
804 .Dl "snprintf(buffer, sizeof(buffer), \*q%s\*q, string);"
805 .Pp
806 The
807 .Nm
808 family of functions currently lack the ability to use the
809 .Cm '
810 flag in conjunction with the
811 .Cm f
812 conversion specifier.
813 The
814 .Cm a
815 and
816 .Cm A
817 conversion specifiers have not yet been implemented.
818 The
819 .Cm l
820 (ell) modifier for the
821 .Cm c
822 and
823 .Cm s
824 conversion specifiers, for wide characters and strings, have not yet
825 been implemented.
826 The
827 .Cm L
828 modifier for floating point formats simply round the
829 .Vt "long double"
830 argument to
831 .Vt double ,
832 providing no additional precision.