]> git.ipfire.org Git - thirdparty/glibc.git/blame - string/bits/string_fortified.h
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / string / bits / string_fortified.h
CommitLineData
04277e02 1/* Copyright (C) 2004-2019 Free Software Foundation, Inc.
b5cc329c
UD
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
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
b5cc329c 17
09a596cc
ZW
18#ifndef _BITS_STRING_FORTIFIED_H
19#define _BITS_STRING_FORTIFIED_H 1
20
b5cc329c 21#ifndef _STRING_H
09a596cc 22# error "Never use <bits/string_fortified.h> directly; include <string.h> instead."
b5cc329c
UD
23#endif
24
1721f0a4 25#if !__GNUC_PREREQ (5,0)
de1c3ebb
UD
26__warndecl (__warn_memset_zero_len,
27 "memset used with constant zero length parameter; this could be due to transposed parameters");
1721f0a4 28#endif
de1c3ebb 29
5ac3ea17 30__fortify_function void *
a784e502 31__NTH (memcpy (void *__restrict __dest, const void *__restrict __src,
de1c3ebb
UD
32 size_t __len))
33{
34 return __builtin___memcpy_chk (__dest, __src, __len, __bos0 (__dest));
35}
b5cc329c 36
5ac3ea17 37__fortify_function void *
a784e502 38__NTH (memmove (void *__dest, const void *__src, size_t __len))
de1c3ebb
UD
39{
40 return __builtin___memmove_chk (__dest, __src, __len, __bos0 (__dest));
41}
b5cc329c
UD
42
43#ifdef __USE_GNU
5ac3ea17 44__fortify_function void *
a784e502 45__NTH (mempcpy (void *__restrict __dest, const void *__restrict __src,
de1c3ebb
UD
46 size_t __len))
47{
48 return __builtin___mempcpy_chk (__dest, __src, __len, __bos0 (__dest));
49}
b5cc329c
UD
50#endif
51
52
553cc5f9
UD
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. */
5ac3ea17 58__fortify_function void *
de1c3ebb
UD
59__NTH (memset (void *__dest, int __ch, size_t __len))
60{
1721f0a4
SP
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)
6515a01f
CM
64 if (__builtin_constant_p (__len) && __len == 0
65 && (!__builtin_constant_p (__ch) || __ch != 0))
de1c3ebb
UD
66 {
67 __warn_memset_zero_len ();
68 return __dest;
69 }
1721f0a4 70#endif
de1c3ebb
UD
71 return __builtin___memset_chk (__dest, __ch, __len, __bos0 (__dest));
72}
b5cc329c 73
498afc54 74#ifdef __USE_MISC
38765ab6 75# include <bits/strings_fortified.h>
75dafa2f
AZ
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}
b5cc329c
UD
85#endif
86
5ac3ea17 87__fortify_function char *
a784e502 88__NTH (strcpy (char *__restrict __dest, const char *__restrict __src))
de1c3ebb
UD
89{
90 return __builtin___strcpy_chk (__dest, __src, __bos (__dest));
91}
b5cc329c
UD
92
93#ifdef __USE_GNU
5ac3ea17 94__fortify_function char *
a784e502 95__NTH (stpcpy (char *__restrict __dest, const char *__restrict __src))
de1c3ebb
UD
96{
97 return __builtin___stpcpy_chk (__dest, __src, __bos (__dest));
98}
b5cc329c
UD
99#endif
100
101
5ac3ea17 102__fortify_function char *
a784e502 103__NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
de1c3ebb
UD
104 size_t __len))
105{
106 return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
107}
b5cc329c 108
458d6339 109/* XXX We have no corresponding builtin yet. */
a784e502 110extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
8215c9ec 111 size_t __destlen) __THROW;
a784e502 112extern char *__REDIRECT_NTH (__stpncpy_alias, (char *__dest, const char *__src,
dc4bb1c2 113 size_t __n), stpncpy);
8215c9ec 114
5ac3ea17 115__fortify_function char *
a784e502 116__NTH (stpncpy (char *__dest, const char *__src, size_t __n))
8215c9ec
UD
117{
118 if (__bos (__dest) != (size_t) -1
8ff5e0ec 119 && (!__builtin_constant_p (__n) || __n > __bos (__dest)))
8215c9ec
UD
120 return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
121 return __stpncpy_alias (__dest, __src, __n);
122}
123
124
5ac3ea17 125__fortify_function char *
a784e502 126__NTH (strcat (char *__restrict __dest, const char *__restrict __src))
de1c3ebb
UD
127{
128 return __builtin___strcat_chk (__dest, __src, __bos (__dest));
129}
b5cc329c
UD
130
131
5ac3ea17 132__fortify_function char *
a784e502 133__NTH (strncat (char *__restrict __dest, const char *__restrict __src,
de1c3ebb
UD
134 size_t __len))
135{
136 return __builtin___strncat_chk (__dest, __src, __len, __bos (__dest));
137}
09a596cc
ZW
138
139#endif /* bits/string_fortified.h */