1/* Copyright (C) 2004-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
17
18#ifndef _BITS_STRING_FORTIFIED_H
19#define _BITS_STRING_FORTIFIED_H 1
20
21#ifndef _STRING_H
22# error "Never use <bits/string_fortified.h> directly; include <string.h> instead."
23#endif
24
25#if !__GNUC_PREREQ (5,0)
26__warndecl (__warn_memset_zero_len,
27 "memset used with constant zero length parameter; this could be due to transposed parameters");
28#endif
29
30__fortify_function void *
31__NTH (memcpy (void *__restrict __dest, const void *__restrict __src,
32 size_t __len))
33{
34 return __builtin___memcpy_chk (__dest, __src, __len, __bos0 (__dest));
35}
36
37__fortify_function void *
38__NTH (memmove (void *__dest, const void *__src, size_t __len))
39{
40 return __builtin___memmove_chk (__dest, __src, __len, __bos0 (__dest));
41}
42
43#ifdef __USE_GNU
44__fortify_function void *
45__NTH (mempcpy (void *__restrict __dest, const void *__restrict __src,
46 size_t __len))
47{
48 return __builtin___mempcpy_chk (__dest, __src, __len, __bos0 (__dest));
49}
50#endif
51
52
53/* The first two tests here help to catch a somewhat common problem
54 where the second and third parameter are transposed. This is
55 especially problematic if the intended fill value is zero. In this
56 case no work is done at all. We detect these problems by referring
57 non-existing functions. */
58__fortify_function void *
59__NTH (memset (void *__dest, int __ch, size_t __len))
60{
61 /* GCC-5.0 and newer implements these checks in the compiler, so we don't
62 need them here. */
63#if !__GNUC_PREREQ (5,0)
64 if (__builtin_constant_p (__len) && __len == 0
65 && (!__builtin_constant_p (__ch) || __ch != 0))
66 {
67 __warn_memset_zero_len ();
68 return __dest;
69 }
70#endif
71 return __builtin___memset_chk (__dest, __ch, __len, __bos0 (__dest));
72}
73
74#ifdef __USE_MISC
75# include <bits/strings_fortified.h>
76
77void __explicit_bzero_chk (void *__dest, size_t __len, size_t __destlen)
78 __THROW __nonnull ((1));
79
80__fortify_function void
81__NTH (explicit_bzero (void *__dest, size_t __len))
82{
83 __explicit_bzero_chk (__dest, __len, __bos0 (__dest));
84}
85#endif
86
87__fortify_function char *
88__NTH (strcpy (char *__restrict __dest, const char *__restrict __src))
89{
90 return __builtin___strcpy_chk (__dest, __src, __bos (__dest));
91}
92
93#ifdef __USE_GNU
94__fortify_function char *
95__NTH (stpcpy (char *__restrict __dest, const char *__restrict __src))
96{
97 return __builtin___stpcpy_chk (__dest, __src, __bos (__dest));
98}
99#endif
100
101
102__fortify_function char *
103__NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
104 size_t __len))
105{
106 return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
107}
108
109/* XXX We have no corresponding builtin yet. */
110extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
111 size_t __destlen) __THROW;
112extern char *__REDIRECT_NTH (__stpncpy_alias, (char *__dest, const char *__src,
113 size_t __n), stpncpy);
114
115__fortify_function char *
116__NTH (stpncpy (char *__dest, const char *__src, size_t __n))
117{
118 if (__bos (__dest) != (size_t) -1
119 && (!__builtin_constant_p (__n) || __n > __bos (__dest)))
120 return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
121 return __stpncpy_alias (__dest, __src, __n);
122}
123
124
125__fortify_function char *
126__NTH (strcat (char *__restrict __dest, const char *__restrict __src))
127{
128 return __builtin___strcat_chk (__dest, __src, __bos (__dest));
129}
130
131
132__fortify_function char *
133__NTH (strncat (char *__restrict __dest, const char *__restrict __src,
134 size_t __len))
135{
136 return __builtin___strncat_chk (__dest, __src, __len, __bos (__dest));
137}
138
139#endif /* bits/string_fortified.h */
140