2015-01-29 11:13:07 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-01-28 15:14:49 +00:00
|
|
|
#ifndef FORTIFY_STRING_H_
|
|
|
|
#define FORTIFY_STRING_H_
|
|
|
|
|
|
|
|
#include_next <string.h>
|
|
|
|
|
|
|
|
#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
|
|
|
|
|
|
|
|
static inline __attribute__ ((always_inline))
|
2015-01-28 23:40:17 +00:00
|
|
|
void *
|
|
|
|
__fortify_memcpy(void *__restrict dest, const void *__restrict src, size_t n)
|
2015-01-28 15:14:49 +00:00
|
|
|
{
|
|
|
|
size_t bos = __builtin_object_size(dest, 0);
|
2015-01-29 10:47:54 +00:00
|
|
|
char *d = dest;
|
|
|
|
const char *s = src;
|
2015-01-28 15:14:49 +00:00
|
|
|
|
2015-01-29 10:47:54 +00:00
|
|
|
/* trap if pointers are overlapping */
|
|
|
|
if ((d <= s && d + n > s) ||
|
|
|
|
(s <= d && s + n > d))
|
|
|
|
__builtin_trap();
|
2015-01-28 17:12:12 +00:00
|
|
|
if (n > bos)
|
2015-01-28 15:36:44 +00:00
|
|
|
__builtin_trap();
|
|
|
|
return memcpy(dest, src, n);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline __attribute__ ((always_inline))
|
2015-01-28 23:40:17 +00:00
|
|
|
void *
|
|
|
|
__fortify_memmove(void *__restrict dest, const void *__restrict src, size_t n)
|
2015-01-28 15:14:49 +00:00
|
|
|
{
|
|
|
|
size_t bos = __builtin_object_size(dest, 0);
|
|
|
|
|
2015-01-28 17:12:12 +00:00
|
|
|
if (n > bos)
|
2015-01-28 15:36:44 +00:00
|
|
|
__builtin_trap();
|
|
|
|
return memmove(dest, src, n);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline __attribute__ ((always_inline))
|
2015-01-28 23:40:17 +00:00
|
|
|
void *
|
|
|
|
__fortify_memset(void *dest, int c, size_t n)
|
2015-01-28 15:14:49 +00:00
|
|
|
{
|
|
|
|
size_t bos = __builtin_object_size(dest, 0);
|
|
|
|
|
2015-01-28 17:12:12 +00:00
|
|
|
if (n > bos)
|
2015-01-28 15:36:44 +00:00
|
|
|
__builtin_trap();
|
|
|
|
return memset(dest, c, n);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 16:16:23 +00:00
|
|
|
static inline __attribute__ ((always_inline))
|
2015-01-28 23:40:17 +00:00
|
|
|
char *
|
|
|
|
__fortify_stpcpy(char *__restrict dest, const char *__restrict src)
|
2015-01-28 16:16:23 +00:00
|
|
|
{
|
|
|
|
size_t bos = __builtin_object_size(dest, 0);
|
|
|
|
|
|
|
|
if (strlen(src) + 1 > bos)
|
|
|
|
__builtin_trap();
|
|
|
|
return stpcpy(dest, src);
|
|
|
|
}
|
|
|
|
|
2015-01-28 16:21:38 +00:00
|
|
|
static inline __attribute__ ((always_inline))
|
2015-01-28 23:40:17 +00:00
|
|
|
char *
|
|
|
|
__fortify_stpncpy(char *__restrict dest, const char *__restrict src, size_t n)
|
2015-01-28 16:21:38 +00:00
|
|
|
{
|
|
|
|
size_t bos = __builtin_object_size(dest, 0);
|
|
|
|
|
2015-01-28 17:12:12 +00:00
|
|
|
if (n > bos)
|
2015-01-28 16:21:38 +00:00
|
|
|
__builtin_trap();
|
|
|
|
return stpncpy(dest, src, n);
|
|
|
|
}
|
|
|
|
|
2015-01-28 15:14:49 +00:00
|
|
|
static inline __attribute__ ((always_inline))
|
2015-01-28 23:40:17 +00:00
|
|
|
char *
|
|
|
|
__fortify_strcat(char *__restrict dest, const char *__restrict src)
|
2015-01-28 15:14:49 +00:00
|
|
|
{
|
|
|
|
size_t bos = __builtin_object_size(dest, 0);
|
|
|
|
|
2015-01-28 15:36:44 +00:00
|
|
|
if (strlen(src) + strlen(dest) + 1 > bos)
|
|
|
|
__builtin_trap();
|
|
|
|
return strcat(dest, src);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline __attribute__ ((always_inline))
|
2015-01-28 23:40:17 +00:00
|
|
|
char *
|
|
|
|
__fortify_strcpy(char *__restrict dest, const char *__restrict src)
|
2015-01-28 15:14:49 +00:00
|
|
|
{
|
|
|
|
size_t bos = __builtin_object_size(dest, 0);
|
|
|
|
|
2015-01-28 15:36:44 +00:00
|
|
|
if (strlen(src) + 1 > bos)
|
|
|
|
__builtin_trap();
|
|
|
|
return strcpy(dest, src);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline __attribute__ ((always_inline))
|
2015-01-28 23:40:17 +00:00
|
|
|
char *
|
|
|
|
__fortify_strncat(char *__restrict dest, const char *__restrict src, size_t n)
|
2015-01-28 15:14:49 +00:00
|
|
|
{
|
|
|
|
size_t bos = __builtin_object_size(dest, 0);
|
2015-01-28 15:36:44 +00:00
|
|
|
size_t slen, dlen;
|
2015-01-28 15:14:49 +00:00
|
|
|
|
2015-01-28 17:12:12 +00:00
|
|
|
if (n > bos) {
|
2015-01-28 15:36:44 +00:00
|
|
|
slen = strlen(src);
|
|
|
|
dlen = strlen(dest);
|
|
|
|
if (slen > n)
|
|
|
|
slen = n;
|
|
|
|
if (slen + dlen + 1 > bos)
|
|
|
|
__builtin_trap();
|
|
|
|
}
|
|
|
|
return strncat(dest, src, n);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline __attribute__ ((always_inline))
|
2015-01-28 23:40:17 +00:00
|
|
|
char *
|
|
|
|
__fortify_strncpy(char *__restrict dest, const char *__restrict src, size_t n)
|
2015-01-28 15:14:49 +00:00
|
|
|
{
|
|
|
|
size_t bos = __builtin_object_size(dest, 0);
|
|
|
|
|
2015-01-28 17:12:12 +00:00
|
|
|
if (n > bos)
|
2015-01-28 15:36:44 +00:00
|
|
|
__builtin_trap();
|
|
|
|
return strncpy(dest, src, n);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 17:47:08 +00:00
|
|
|
#ifdef _GNU_SOURCE
|
2015-01-28 17:44:38 +00:00
|
|
|
static inline __attribute__ ((always_inline))
|
2015-01-28 23:40:17 +00:00
|
|
|
void *
|
|
|
|
__fortify_mempcpy(void *__restrict dest, const void *__restrict src, size_t n)
|
2015-01-28 17:44:38 +00:00
|
|
|
{
|
|
|
|
size_t bos = __builtin_object_size(dest, 0);
|
|
|
|
|
|
|
|
if (n > bos)
|
|
|
|
__builtin_trap();
|
|
|
|
return mempcpy(dest, src, n);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-28 15:14:49 +00:00
|
|
|
#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
|
|
|
|
static inline __attribute__ ((always_inline))
|
2015-01-28 23:40:17 +00:00
|
|
|
size_t
|
|
|
|
__fortify_strlcat(char *__restrict dest, const char *__restrict src, size_t n)
|
2015-01-28 15:14:49 +00:00
|
|
|
{
|
|
|
|
size_t bos = __builtin_object_size(dest, 0);
|
|
|
|
|
2015-01-28 17:12:12 +00:00
|
|
|
if (n > bos)
|
2015-01-28 15:36:44 +00:00
|
|
|
__builtin_trap();
|
|
|
|
return strlcat(dest, src, n);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline __attribute__ ((always_inline))
|
2015-01-28 23:40:17 +00:00
|
|
|
size_t
|
|
|
|
__fortify_strlcpy(char *__restrict dest, const char *__restrict src, size_t n)
|
2015-01-28 15:14:49 +00:00
|
|
|
{
|
|
|
|
size_t bos = __builtin_object_size(dest, 0);
|
|
|
|
|
2015-01-28 17:12:12 +00:00
|
|
|
if (n > bos)
|
2015-01-28 15:36:44 +00:00
|
|
|
__builtin_trap();
|
|
|
|
return strlcpy(dest, src, n);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
2015-01-28 16:07:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef memcpy
|
|
|
|
#define memcpy(dest, src, n) __fortify_memcpy(dest, src, n)
|
|
|
|
#undef memmove
|
|
|
|
#define memmove(dest, src, n) __fortify_memmove(dest, src, n)
|
|
|
|
#undef memset
|
|
|
|
#define memset(dest, src, n) __fortify_memset(dest, src, n)
|
2015-01-28 16:16:23 +00:00
|
|
|
#undef stpcpy
|
2015-01-28 16:31:19 +00:00
|
|
|
#define stpcpy(dest, src) __fortify_stpcpy(dest, src)
|
2015-01-28 16:21:38 +00:00
|
|
|
#undef stpncpy
|
2015-01-28 16:31:19 +00:00
|
|
|
#define stpncpy(dest, src, n) __fortify_stpncpy(dest, src, n)
|
2015-01-28 16:07:24 +00:00
|
|
|
#undef strcat
|
|
|
|
#define strcat(dest, src) __fortify_strcat(dest, src)
|
|
|
|
#undef strcpy
|
|
|
|
#define strcpy(dest, src) __fortify_strcpy(dest, src)
|
|
|
|
#undef strncat
|
|
|
|
#define strncat(dest, src, n) __fortify_strcat(dest, src, n)
|
|
|
|
#undef strncpy
|
|
|
|
#define strncpy(dest, src, n) __fortify_strcpy(dest, src, n)
|
2015-01-28 15:14:49 +00:00
|
|
|
|
2015-01-28 17:47:08 +00:00
|
|
|
#ifdef _GNU_SOURCE
|
2015-01-28 17:44:38 +00:00
|
|
|
#undef mempcpy
|
|
|
|
#define mempcpy(dest, src, n) __fortify_mempcpy(dest, src, n)
|
|
|
|
#endif
|
|
|
|
|
2015-01-28 16:07:24 +00:00
|
|
|
#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
|
|
|
|
#undef strlcat
|
|
|
|
#define strlcat(dest, src, n) __fortify_strlcat(dest, src, n)
|
2015-01-28 15:14:49 +00:00
|
|
|
#undef strlcpy
|
|
|
|
#define strlcpy(dest, src, n) __fortify_strlcpy(dest, src, n)
|
|
|
|
#endif
|
2015-01-28 16:07:24 +00:00
|
|
|
|
2015-01-28 15:14:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|