freebsd-src/include/ssp/wchar.h
Kyle Evans de866aa352 ssp: switch to _limits.h for the upper bound of size_t
The definitions in _stdint.h has some complications around visibility
that _limits.h does not have.  Switch to __SIZE_T_MAX to avoid those.

This fixes the devel/gperf, devel/glib20 and math/mpfr builds with
_FORTIFY_SOURCE enabled to unlock a large fraction of the ports tree to
build.

Reported by:	Shawn Webb (HardenedBSD)
Sponsored by:	Klara, Inc.
Sponsored by:	Stormshield
2024-08-01 15:42:17 -05:00

230 lines
5.7 KiB
C

/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2024, Klara, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SSP_WCHAR_H_
#define _SSP_WCHAR_H_
#include <ssp/ssp.h>
#if __SSP_FORTIFY_LEVEL > 0
__ssp_inline int
__ssp_wchar_overlap(const void *leftp, const void *rightp, size_t len)
{
if (len > __SIZE_T_MAX / sizeof(wchar_t))
return (1);
return (__ssp_overlap(leftp, rightp, len * sizeof(wchar_t)));
}
/*
* __ssp_wbos for w*() calls where the size parameters are in sizeof(wchar_t)
* units, so the result needs to be scaled appropriately.
*/
__ssp_inline size_t
__ssp_wbos(void *ptr)
{
const size_t ptrsize = __ssp_bos(ptr);
if (ptrsize == (size_t)-1)
return (ptrsize);
return (ptrsize / sizeof(wchar_t));
}
__BEGIN_DECLS
__ssp_redirect_raw_impl(wchar_t *, wmemcpy, wmemcpy,
(wchar_t *__restrict buf, const wchar_t *__restrict src, size_t len))
{
const size_t slen = __ssp_wbos(buf);
if (len > slen)
__chk_fail();
if (__ssp_wchar_overlap(src, buf, len))
__chk_fail();
return (__ssp_real(wmemcpy)(buf, src, len));
}
__ssp_redirect_raw_impl(wchar_t *, wmempcpy, wmempcpy,
(wchar_t *__restrict buf, const wchar_t *__restrict src, size_t len))
{
const size_t slen = __ssp_wbos(buf);
if (len > slen)
__chk_fail();
if (__ssp_wchar_overlap(src, buf, len))
__chk_fail();
return (__ssp_real(wmempcpy)(buf, src, len));
}
__ssp_redirect_raw_impl(wchar_t *, wmemmove, wmemmove,
(wchar_t *buf, const wchar_t *src, size_t len))
{
const size_t slen = __ssp_wbos(buf);
if (len > slen)
__chk_fail();
return (__ssp_real(wmemmove)(buf, src, len));
}
__ssp_redirect_raw_impl(wchar_t *, wmemset, wmemset,
(wchar_t *buf, wchar_t c, size_t len))
{
const size_t slen = __ssp_wbos(buf);
if (len > slen)
__chk_fail();
return (__ssp_real(wmemset)(buf, c, len));
}
__ssp_redirect_raw_impl(wchar_t *, wcpcpy, wcpcpy,
(wchar_t *__restrict buf, const wchar_t *__restrict src))
{
const size_t slen = __ssp_wbos(buf);
const size_t len = wcslen(src);
if (len >= slen)
__chk_fail();
if (__ssp_wchar_overlap(buf, src, len))
__chk_fail();
(void)__ssp_real(wmemcpy)(buf, src, len + 1);
return (buf + len);
}
__ssp_redirect_raw_impl(wchar_t *, wcpncpy, wcpncpy,
(wchar_t *__restrict buf, const wchar_t *__restrict src, size_t len))
{
const size_t slen = __ssp_wbos(buf);
if (len > slen)
__chk_fail();
if (__ssp_wchar_overlap(buf, src, len))
__chk_fail();
return (__ssp_real(wcpncpy)(buf, src, len));
}
__ssp_redirect_raw_impl(wchar_t *, wcscat, wcscat,
(wchar_t *__restrict buf, const wchar_t *__restrict src))
{
size_t slen = __ssp_wbos(buf);
wchar_t *cp;
cp = buf;
while (*cp != L'\0') {
cp++;
if (slen-- == 0)
__chk_fail();
}
while (*src != L'\0') {
if (slen-- == 0)
__chk_fail();
*cp++ = *src++;
}
if (slen-- == 0)
__chk_fail();
*cp = '\0';
return (buf);
}
__ssp_redirect_raw_impl(wchar_t *, wcscpy, wcscpy,
(wchar_t *__restrict buf, const wchar_t *__restrict src))
{
const size_t slen = __ssp_wbos(buf);
size_t len = wcslen(src) + 1;
if (len > slen)
__chk_fail();
if (__ssp_wchar_overlap(buf, src, len))
__chk_fail();
return (__ssp_real(wmemcpy)(buf, src, len));
}
__ssp_redirect_raw_impl(wchar_t *, wcsncat, wcsncat,
(wchar_t *__restrict buf, const wchar_t *__restrict src, size_t len))
{
const size_t slen = __ssp_wbos(buf);
if (len == 0)
return (buf);
if (len > slen)
__chk_fail();
if (__ssp_wchar_overlap(buf, src, len))
__chk_fail();
return (__ssp_real(wcsncat)(buf, src, len));
}
__ssp_redirect_raw_impl(size_t, wcslcat, wcslcat,
(wchar_t *__restrict buf, const wchar_t *__restrict src, size_t len))
{
const size_t slen = __ssp_wbos(buf);
if (len > slen)
__chk_fail();
if (__ssp_wchar_overlap(buf, src, len))
__chk_fail();
return (__ssp_real(wcslcat)(buf, src, len));
}
__ssp_redirect_raw_impl(wchar_t *, wcsncpy, wcsncpy,
(wchar_t *__restrict buf, const wchar_t *__restrict src, size_t len))
{
const size_t slen = __ssp_wbos(buf);
if (len > slen)
__chk_fail();
if (__ssp_wchar_overlap(buf, src, len))
__chk_fail();
return (__ssp_real(wcsncpy)(buf, src, len));
}
__ssp_redirect_raw_impl(size_t, wcslcpy, wcslcpy,
(wchar_t *__restrict buf, const wchar_t *__restrict src, size_t len))
{
const size_t slen = __ssp_wbos(buf);
if (len > slen)
__chk_fail();
if (__ssp_wchar_overlap(buf, src, len))
__chk_fail();
return (__ssp_real(wcslcpy)(buf, src, len));
}
__END_DECLS
#endif /* __SSP_FORTIFY_LEVEL > 0 */
#endif /* _SSP_WCHAR_H_ */