xref: /netbsd/lib/libc/gen/fmtcheck.3 (revision c4a72b64)
1.\"	$NetBSD: fmtcheck.3,v 1.4 2002/10/01 19:47:40 wiz Exp $
2.\"
3.\" Copyright (c) 2000 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This file was contributed to The NetBSD Foundation by Allen Briggs.
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 NetBSD
19.\"        Foundation, Inc. and its contributors.
20.\" 4. Neither the name of The NetBSD Foundation nor the names of its
21.\"    contributors may be used to endorse or promote products derived
22.\"    from this software without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34.\" POSSIBILITY OF SUCH DAMAGE.
35.\"
36.Dd October 17, 2000
37.Os
38.Dt FMTCHECK 3
39.Sh NAME
40.Nm fmtcheck
41.Nd sanitizes user-supplied printf(3)-style format string
42.Sh LIBRARY
43.Lb libc
44.Sh SYNOPSIS
45.Fd #include \*[Lt]stdio.h\*[Gt]
46.Ft const char *
47.Fn fmtcheck "const char *fmt_suspect" "const char *fmt_default"
48.Sh DESCRIPTION
49The
50.Nm
51function scans
52.Fa fmt_suspect
53and
54.Fa fmt_default
55to determine if
56.Fa fmt_suspect
57will consume the same argument types as
58.Fa fmt_default
59and to ensure that
60.Fa fmt_suspect
61is a valid format string.
62.Pp
63The
64.Xr printf 3
65family of functions can not verify the types of arguments that they are
66passed at run-time.
67In some cases, like
68.Xr catgets 3 ,
69it is useful or necessary to use a user-supplied format string with no
70guarantee that the format string matches the specified parameters.
71.Pp
72The
73.Nm
74function was designed to be used in these cases, as in:
75.Bd -literal -offset indent
76printf(fmtcheck(user_format, standard_format), arg1, arg2);
77.Ed
78.Pp
79In the check, field widths, fillers, precisions, etc. are ignored (unless
80the field width or precision is an asterisk
81.Ql *
82instead of a digit string).
83Also, any text other than the format specifiers is completely ignored.
84.Pp
85Note that the formats may be quite different as long as they accept the
86same parameters.
87For example, "%p %o %30s %#llx %-10.*e %n" is
88compatible with "This number %lu %d%% and string %s has %qd numbers
89and %.*g floats (%n)."
90However, "%o" is not equivalent to "%lx" because
91the first requires an integer and the second requires a long.
92.Sh RETURN VALUES
93If
94.Fa fmt_suspect
95is a valid format and consumes the same argument types as
96.Fa fmt_default ,
97then the
98.Nm
99function will return
100.Fa fmt_suspect .
101Otherwise, it will return
102.Fa fmt_default .
103.Sh SEE ALSO
104.Xr printf 3
105