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