1fmtcheck(3bsd) LOCAL fmtcheck(3bsd)
2
4 fmtcheck — sanitizes user-supplied printf(3)-style format string
5
7 Utility functions from BSD systems (libbsd, -lbsd)
8
10 #include <stdio.h>
11 (See libbsd(7) for include usage.)
12
13 const char *
14 fmtcheck(const char *fmt_suspect, const char *fmt_default);
15
17 The fmtcheck function scans fmt_suspect and fmt_default to determine if
18 fmt_suspect will consume the same argument types as fmt_default and to
19 ensure that fmt_suspect is a valid format string.
20
21 The printf(3) family of functions can not verify the types of arguments
22 that they are passed at run-time. In some cases, like catgets(3), it is
23 useful or necessary to use a user-supplied format string with no guaran‐
24 tee that the format string matches the specified parameters.
25
26 The fmtcheck function was designed to be used in these cases, as in:
27
28 printf(fmtcheck(user_format, standard_format), arg1, arg2);
29
30 In the check, field widths, fillers, precisions, etc. are ignored (unless
31 the field width or precision is an asterisk ‘*’ instead of a digit
32 string). Also, any text other than the format specifiers is completely
33 ignored.
34
35 Note that the formats may be quite different as long as they accept the
36 same parameters. For example, "%ld %o %30s %#llx %-10.*e %n" is compati‐
37 ble with "This number %lu %d%% and string %s has %qd numbers and %.*g
38 floats (%n)." However, "%o" is not equivalent to "%lx" because the first
39 requires an integer and the second requires a long, and "%p" is not
40 equivalent to "%lu" because the first requires a pointer and the second
41 requires a long.
42
44 If fmt_suspect is a valid format and consumes the same argument types as
45 fmt_default, then the fmtcheck function will return fmt_suspect. Other‐
46 wise, it will return fmt_default.
47
49 printf(3)
50
51BSD June 14, 2014 BSD