xref: /freebsd/lib/libc/secure/strncpy_chk.c (revision be04fec4)
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: strncpy_chk.c,v 1.6 2015/05/09 15:42:21 christos Exp $");
34*be04fec4SKyle Evans 
35*be04fec4SKyle Evans #include <string.h>
36*be04fec4SKyle Evans 
37*be04fec4SKyle Evans #include <ssp/string.h>
38*be04fec4SKyle Evans #undef strncpy
39*be04fec4SKyle Evans 
40*be04fec4SKyle Evans #include "ssp_internal.h"
41*be04fec4SKyle Evans 
42*be04fec4SKyle Evans char *
__strncpy_chk(char * __restrict dst,const char * __restrict src,size_t len,size_t slen)43*be04fec4SKyle Evans __strncpy_chk(char * __restrict dst, const char * __restrict src, size_t len,
44*be04fec4SKyle Evans     size_t slen)
45*be04fec4SKyle Evans {
46*be04fec4SKyle Evans 	if (len > slen)
47*be04fec4SKyle Evans 		__chk_fail();
48*be04fec4SKyle Evans 
49*be04fec4SKyle Evans 	if (__ssp_overlap(src, dst, len))
50*be04fec4SKyle Evans 		__chk_fail();
51*be04fec4SKyle Evans 
52*be04fec4SKyle Evans 	return (strncpy(dst, src, len));
53*be04fec4SKyle Evans }
54