2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2024, Klara, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
32 #if __SSP_FORTIFY_LEVEL > 0
35 __ssp_wchar_overlap(const void *leftp
, const void *rightp
, size_t len
)
38 if (len
> __SIZE_T_MAX
/ sizeof(wchar_t))
40 return (__ssp_overlap(leftp
, rightp
, len
* sizeof(wchar_t)));
44 * __ssp_wbos for w*() calls where the size parameters are in sizeof(wchar_t)
45 * units, so the result needs to be scaled appropriately.
50 const size_t ptrsize
= __ssp_bos(ptr
);
52 if (ptrsize
== (size_t)-1)
55 return (ptrsize
/ sizeof(wchar_t));
59 __ssp_redirect_raw_impl(wchar_t *, wmemcpy
, wmemcpy
,
60 (wchar_t *__restrict buf
, const wchar_t *__restrict src
, size_t len
))
62 const size_t slen
= __ssp_wbos(buf
);
66 if (__ssp_wchar_overlap(src
, buf
, len
))
69 return (__ssp_real(wmemcpy
)(buf
, src
, len
));
72 __ssp_redirect_raw_impl(wchar_t *, wmempcpy
, wmempcpy
,
73 (wchar_t *__restrict buf
, const wchar_t *__restrict src
, size_t len
))
75 const size_t slen
= __ssp_wbos(buf
);
79 if (__ssp_wchar_overlap(src
, buf
, len
))
82 return (__ssp_real(wmempcpy
)(buf
, src
, len
));
85 __ssp_redirect_raw_impl(wchar_t *, wmemmove
, wmemmove
,
86 (wchar_t *buf
, const wchar_t *src
, size_t len
))
88 const size_t slen
= __ssp_wbos(buf
);
93 return (__ssp_real(wmemmove
)(buf
, src
, len
));
96 __ssp_redirect_raw_impl(wchar_t *, wmemset
, wmemset
,
97 (wchar_t *buf
, wchar_t c
, size_t len
))
99 const size_t slen
= __ssp_wbos(buf
);
103 return (__ssp_real(wmemset
)(buf
, c
, len
));
106 __ssp_redirect_raw_impl(wchar_t *, wcpcpy
, wcpcpy
,
107 (wchar_t *__restrict buf
, const wchar_t *__restrict src
))
109 const size_t slen
= __ssp_wbos(buf
);
110 const size_t len
= wcslen(src
);
114 if (__ssp_wchar_overlap(buf
, src
, len
))
117 (void)__ssp_real(wmemcpy
)(buf
, src
, len
+ 1);
121 __ssp_redirect_raw_impl(wchar_t *, wcpncpy
, wcpncpy
,
122 (wchar_t *__restrict buf
, const wchar_t *__restrict src
, size_t len
))
124 const size_t slen
= __ssp_wbos(buf
);
128 if (__ssp_wchar_overlap(buf
, src
, len
))
131 return (__ssp_real(wcpncpy
)(buf
, src
, len
));
134 __ssp_redirect_raw_impl(wchar_t *, wcscat
, wcscat
,
135 (wchar_t *__restrict buf
, const wchar_t *__restrict src
))
137 size_t slen
= __ssp_wbos(buf
);
141 while (*cp
!= L
'\0') {
147 while (*src
!= L
'\0') {
159 __ssp_redirect_raw_impl(wchar_t *, wcscpy
, wcscpy
,
160 (wchar_t *__restrict buf
, const wchar_t *__restrict src
))
162 const size_t slen
= __ssp_wbos(buf
);
163 size_t len
= wcslen(src
) + 1;
167 if (__ssp_wchar_overlap(buf
, src
, len
))
170 return (__ssp_real(wmemcpy
)(buf
, src
, len
));
173 __ssp_redirect_raw_impl(wchar_t *, wcsncat
, wcsncat
,
174 (wchar_t *__restrict buf
, const wchar_t *__restrict src
, size_t len
))
176 const size_t slen
= __ssp_wbos(buf
);
182 if (__ssp_wchar_overlap(buf
, src
, len
))
185 return (__ssp_real(wcsncat
)(buf
, src
, len
));
188 __ssp_redirect_raw_impl(size_t, wcslcat
, wcslcat
,
189 (wchar_t *__restrict buf
, const wchar_t *__restrict src
, size_t len
))
191 const size_t slen
= __ssp_wbos(buf
);
195 if (__ssp_wchar_overlap(buf
, src
, len
))
198 return (__ssp_real(wcslcat
)(buf
, src
, len
));
201 __ssp_redirect_raw_impl(wchar_t *, wcsncpy
, wcsncpy
,
202 (wchar_t *__restrict buf
, const wchar_t *__restrict src
, size_t len
))
204 const size_t slen
= __ssp_wbos(buf
);
208 if (__ssp_wchar_overlap(buf
, src
, len
))
211 return (__ssp_real(wcsncpy
)(buf
, src
, len
));
214 __ssp_redirect_raw_impl(size_t, wcslcpy
, wcslcpy
,
215 (wchar_t *__restrict buf
, const wchar_t *__restrict src
, size_t len
))
217 const size_t slen
= __ssp_wbos(buf
);
221 if (__ssp_wchar_overlap(buf
, src
, len
))
224 return (__ssp_real(wcslcpy
)(buf
, src
, len
));
228 #endif /* __SSP_FORTIFY_LEVEL > 0 */
229 #endif /* _SSP_WCHAR_H_ */