]>
git.ipfire.org Git - thirdparty/glibc.git/blob - string/bits/string_fortified.h
1 /* Copyright (C) 2004-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
18 #ifndef _BITS_STRING_FORTIFIED_H
19 #define _BITS_STRING_FORTIFIED_H 1
22 # error "Never use <bits/string_fortified.h> directly; include <string.h> instead."
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");
30 __fortify_function
void *
31 __NTH (memcpy (void *__restrict __dest
, const void *__restrict __src
,
34 return __builtin___memcpy_chk (__dest
, __src
, __len
, __bos0 (__dest
));
37 __fortify_function
void *
38 __NTH (memmove (void *__dest
, const void *__src
, size_t __len
))
40 return __builtin___memmove_chk (__dest
, __src
, __len
, __bos0 (__dest
));
44 __fortify_function
void *
45 __NTH (mempcpy (void *__restrict __dest
, const void *__restrict __src
,
48 return __builtin___mempcpy_chk (__dest
, __src
, __len
, __bos0 (__dest
));
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
))
61 /* GCC-5.0 and newer implements these checks in the compiler, so we don't
63 #if !__GNUC_PREREQ (5,0)
64 if (__builtin_constant_p (__len
) && __len
== 0
65 && (!__builtin_constant_p (__ch
) || __ch
!= 0))
67 __warn_memset_zero_len ();
71 return __builtin___memset_chk (__dest
, __ch
, __len
, __bos0 (__dest
));
75 # include <bits/strings_fortified.h>
77 void __explicit_bzero_chk (void *__dest
, size_t __len
, size_t __destlen
)
78 __THROW
__nonnull ((1));
80 __fortify_function
void
81 __NTH (explicit_bzero (void *__dest
, size_t __len
))
83 __explicit_bzero_chk (__dest
, __len
, __bos0 (__dest
));
87 __fortify_function
char *
88 __NTH (strcpy (char *__restrict __dest
, const char *__restrict __src
))
90 return __builtin___strcpy_chk (__dest
, __src
, __bos (__dest
));
94 __fortify_function
char *
95 __NTH (stpcpy (char *__restrict __dest
, const char *__restrict __src
))
97 return __builtin___stpcpy_chk (__dest
, __src
, __bos (__dest
));
102 __fortify_function
char *
103 __NTH (strncpy (char *__restrict __dest
, const char *__restrict __src
,
106 return __builtin___strncpy_chk (__dest
, __src
, __len
, __bos (__dest
));
109 /* XXX We have no corresponding builtin yet. */
110 extern char *__stpncpy_chk (char *__dest
, const char *__src
, size_t __n
,
111 size_t __destlen
) __THROW
;
112 extern char *__REDIRECT_NTH (__stpncpy_alias
, (char *__dest
, const char *__src
,
113 size_t __n
), stpncpy
);
115 __fortify_function
char *
116 __NTH (stpncpy (char *__dest
, const char *__src
, size_t __n
))
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
);
125 __fortify_function
char *
126 __NTH (strcat (char *__restrict __dest
, const char *__restrict __src
))
128 return __builtin___strcat_chk (__dest
, __src
, __bos (__dest
));
132 __fortify_function
char *
133 __NTH (strncat (char *__restrict __dest
, const char *__restrict __src
,
136 return __builtin___strncat_chk (__dest
, __src
, __len
, __bos (__dest
));
139 #endif /* bits/string_fortified.h */