fortify-headers/include/string.h

196 lines
4.3 KiB
C
Raw Normal View History

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
#ifndef __cplusplus
2015-03-11 09:26:11 +00:00
2015-01-28 15:14:49 +00:00
static inline __attribute__ ((always_inline))
2015-01-28 23:40:17 +00:00
void *
2015-01-30 16:25:13 +00:00
__fortify_memcpy(void *dest, const void *src, size_t n)
2015-01-28 15:14:49 +00:00
{
size_t bos = __builtin_object_size(dest, 0);
2015-03-11 09:21:25 +00:00
char *d = (char *)dest;
const char *s = (const char *)src;
2015-01-28 15:14:49 +00:00
2015-02-25 10:35:16 +00:00
/* trap if pointers are overlapping but not if dest == src.
* gcc seems to like to generate code that relies on dest == src */
if ((d < s && d + n > s) ||
(s < d && s + n > d))
__builtin_trap();
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 *
2015-01-30 16:25:13 +00:00
__fortify_memmove(void *dest, const void *src, size_t n)
2015-01-28 15:14:49 +00:00
{
size_t bos = __builtin_object_size(dest, 0);
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);
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 *
2015-01-30 16:25:13 +00:00
__fortify_stpcpy(char *dest, const char *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 *
2015-01-30 16:25:13 +00:00
__fortify_stpncpy(char *dest, const char *src, size_t n)
2015-01-28 16:21:38 +00:00
{
size_t bos = __builtin_object_size(dest, 0);
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 *
2015-01-30 16:25:13 +00:00
__fortify_strcat(char *dest, const char *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 *
2015-01-30 16:25:13 +00:00
__fortify_strcpy(char *dest, const char *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 *
2015-01-30 16:25:13 +00:00
__fortify_strncat(char *dest, const char *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
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 *
2015-01-30 16:25:13 +00:00
__fortify_strncpy(char *dest, const char *src, size_t n)
2015-01-28 15:14:49 +00:00
{
size_t bos = __builtin_object_size(dest, 0);
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 *
2015-01-30 16:25:13 +00:00
__fortify_mempcpy(void *dest, const void *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
2015-01-30 16:25:13 +00:00
__fortify_strlcat(char *dest, const char *src, size_t n)
2015-01-28 15:14:49 +00:00
{
size_t bos = __builtin_object_size(dest, 0);
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
2015-01-30 16:25:13 +00:00
__fortify_strlcpy(char *dest, const char *src, size_t n)
2015-01-28 15:14:49 +00:00
{
size_t bos = __builtin_object_size(dest, 0);
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
}
#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)
#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_strncat(dest, src, n)
#undef strncpy
#define strncpy(dest, src, n) __fortify_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
#undef mempcpy
#define mempcpy(dest, src, n) __fortify_mempcpy(dest, src, n)
#endif
#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-03-11 09:26:11 +00:00
#endif
2015-01-28 15:14:49 +00:00
#endif
#endif