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