xref: /freebsd/lib/libc/secure/sprintf_chk.c (revision be04fec42638f30f50b5b55fd8e3634c0fb89928)
1*be04fec4SKyle Evans /*-
2*be04fec4SKyle Evans  *
3*be04fec4SKyle Evans  * SPDX-License-Identifier: BSD-2-Clause
4*be04fec4SKyle Evans  *
5*be04fec4SKyle Evans  * Copyright (c) 2006 The NetBSD Foundation, Inc.
6*be04fec4SKyle Evans  * All rights reserved.
7*be04fec4SKyle Evans  *
8*be04fec4SKyle Evans  * This code is derived from software contributed to The NetBSD Foundation
9*be04fec4SKyle Evans  * by Christos Zoulas.
10*be04fec4SKyle Evans  *
11*be04fec4SKyle Evans  * Redistribution and use in source and binary forms, with or without
12*be04fec4SKyle Evans  * modification, are permitted provided that the following conditions
13*be04fec4SKyle Evans  * are met:
14*be04fec4SKyle Evans  * 1. Redistributions of source code must retain the above copyright
15*be04fec4SKyle Evans  *    notice, this list of conditions and the following disclaimer.
16*be04fec4SKyle Evans  * 2. Redistributions in binary form must reproduce the above copyright
17*be04fec4SKyle Evans  *    notice, this list of conditions and the following disclaimer in the
18*be04fec4SKyle Evans  *    documentation and/or other materials provided with the distribution.
19*be04fec4SKyle Evans  *
20*be04fec4SKyle Evans  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21*be04fec4SKyle Evans  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22*be04fec4SKyle Evans  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23*be04fec4SKyle Evans  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24*be04fec4SKyle Evans  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25*be04fec4SKyle Evans  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26*be04fec4SKyle Evans  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27*be04fec4SKyle Evans  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28*be04fec4SKyle Evans  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*be04fec4SKyle Evans  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30*be04fec4SKyle Evans  * POSSIBILITY OF SUCH DAMAGE.
31*be04fec4SKyle Evans  */
32*be04fec4SKyle Evans #include <sys/cdefs.h>
33*be04fec4SKyle Evans __RCSID("$NetBSD: sprintf_chk.c,v 1.6 2009/02/05 05:40:36 lukem Exp $");
34*be04fec4SKyle Evans 
35*be04fec4SKyle Evans #include <limits.h>
36*be04fec4SKyle Evans #include <stdarg.h>
37*be04fec4SKyle Evans #include <stdio.h>
38*be04fec4SKyle Evans 
39*be04fec4SKyle Evans #include <ssp/stdio.h>
40*be04fec4SKyle Evans #undef vsnprintf
41*be04fec4SKyle Evans #undef vsprintf
42*be04fec4SKyle Evans 
43*be04fec4SKyle Evans int
__sprintf_chk(char * __restrict buf,int flags,size_t slen,const char * __restrict fmt,...)44*be04fec4SKyle Evans __sprintf_chk(char * __restrict buf, int flags, size_t slen,
45*be04fec4SKyle Evans     const char * __restrict fmt, ...)
46*be04fec4SKyle Evans {
47*be04fec4SKyle Evans 	va_list ap;
48*be04fec4SKyle Evans 	int rv;
49*be04fec4SKyle Evans 
50*be04fec4SKyle Evans 	va_start(ap, fmt);
51*be04fec4SKyle Evans 	if (slen > (size_t)INT_MAX)
52*be04fec4SKyle Evans 		rv = vsprintf(buf, fmt, ap);
53*be04fec4SKyle Evans 	else {
54*be04fec4SKyle Evans 		if ((rv = vsnprintf(buf, slen, fmt, ap)) >= 0 &&
55*be04fec4SKyle Evans 		    (size_t)rv >= slen)
56*be04fec4SKyle Evans 			__chk_fail();
57*be04fec4SKyle Evans 	}
58*be04fec4SKyle Evans 	va_end(ap);
59*be04fec4SKyle Evans 
60*be04fec4SKyle Evans 	return (rv);
61*be04fec4SKyle Evans }
62