1.\" $NetBSD: fmtcheck.3,v 1.2 2002/02/07 07:00:11 ross 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. In some cases, like 67.Xr catgets 3 , 68it is useful or necessary to use a user-supplied format string with no 69guarantee that the format string matches the specified parameters. 70.Pp 71The 72.Nm 73function was designed to be used in these cases, as in: 74.Bd -literal -offset indent 75printf(fmtcheck(user_format, standard_format), arg1, arg2); 76.Ed 77.Pp 78In the check, field widths, fillers, precisions, etc. are ignored (unless 79the field width or precision is an asterisk 80.Ql * 81instead of a digit string). Also, any text other than the format specifiers 82is completely ignored. 83.Pp 84Note that the formats may be quite different as long as they accept the 85same parameters. For example, ".Dq %p %o %30s %#llx %-10.*e %n" is 86compatible with "This number %lu %d%% and string %s has %qd numbers 87and %.*g floats (%n)." However, "%o" is not equivalent to "%lx" because 88the first requires an integer and the second requires a long. 89.Sh RETURN VALUES 90If 91.Fa fmt_suspect 92is a valid format and consumes the same argument types as 93.Fa fmt_default , 94then the 95.Nm 96function will return 97.Fa fmt_suspect . 98Otherwise, it will return 99.Fa fmt_default . 100.Sh SEE ALSO 101.Xr printf 3 102