2015-03-11 18:55:51 +00:00
|
|
|
#ifndef _FORTIFY_STRING_H
|
|
|
|
#define _FORTIFY_STRING_H
|
2015-01-28 15:14:49 +00:00
|
|
|
|
|
|
|
#include_next <string.h>
|
|
|
|
|
|
|
|
#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
|
|
|
|
|
2015-03-13 23:09:15 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-03-13 11:00:46 +00:00
|
|
|
#undef memcpy
|
|
|
|
#undef memmove
|
|
|
|
#undef memset
|
|
|
|
#undef stpcpy
|
|
|
|
#undef stpncpy
|
|
|
|
#undef strcat
|
|
|
|
#undef strcpy
|
|
|
|
#undef strncat
|
|
|
|
#undef strncpy
|
2015-03-11 09:26:11 +00:00
|
|
|
|
2015-03-14 19:06:48 +00:00
|
|
|
__typeof__(memcpy) __memcpy_orig __asm__(__USER_LABEL_PREFIX__ "memcpy");
|
2015-03-14 09:38:11 +00:00
|
|
|
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
|
2015-03-13 11:00:46 +00:00
|
|
|
void *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-14 09:45:37 +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 */
|
2015-03-11 09:21:00 +00:00
|
|
|
if ((d < s && d + n > s) ||
|
|
|
|
(s < d && s + n > d))
|
2015-01-29 10:47:54 +00:00
|
|
|
__builtin_trap();
|
2015-01-28 17:12:12 +00:00
|
|
|
if (n > bos)
|
2015-01-28 15:36:44 +00:00
|
|
|
__builtin_trap();
|
2015-03-13 11:00:46 +00:00
|
|
|
return __memcpy_orig(dest, src, n);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 19:06:48 +00:00
|
|
|
__typeof__(memmove) __memmove_orig __asm__(__USER_LABEL_PREFIX__ "memmove");
|
2015-03-14 09:38:11 +00:00
|
|
|
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
|
2015-03-13 11:00:46 +00:00
|
|
|
void *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);
|
|
|
|
|
2015-01-28 17:12:12 +00:00
|
|
|
if (n > bos)
|
2015-01-28 15:36:44 +00:00
|
|
|
__builtin_trap();
|
2015-03-13 11:00:46 +00:00
|
|
|
return __memmove_orig(dest, src, n);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 19:06:48 +00:00
|
|
|
__typeof__(memset) __memset_orig __asm__(__USER_LABEL_PREFIX__ "memset");
|
2015-03-14 09:38:11 +00:00
|
|
|
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
|
2015-03-13 11:00:46 +00:00
|
|
|
void *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();
|
2015-03-13 11:00:46 +00:00
|
|
|
return __memset_orig(dest, c, n);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 19:06:48 +00:00
|
|
|
__typeof__(stpcpy) __stpcpy_orig __asm__(__USER_LABEL_PREFIX__ "stpcpy");
|
2015-03-14 09:38:11 +00:00
|
|
|
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
|
2015-03-13 11:00:46 +00:00
|
|
|
char *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();
|
2015-03-13 11:00:46 +00:00
|
|
|
return __stpcpy_orig(dest, src);
|
2015-01-28 16:16:23 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 19:06:48 +00:00
|
|
|
__typeof__(stpncpy) __stpncpy_orig __asm__(__USER_LABEL_PREFIX__ "stpncpy");
|
2015-03-14 09:38:11 +00:00
|
|
|
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
|
2015-03-13 11:00:46 +00:00
|
|
|
char *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);
|
|
|
|
|
2015-01-28 17:12:12 +00:00
|
|
|
if (n > bos)
|
2015-01-28 16:21:38 +00:00
|
|
|
__builtin_trap();
|
2015-03-13 11:00:46 +00:00
|
|
|
return __stpncpy_orig(dest, src, n);
|
2015-01-28 16:21:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 19:06:48 +00:00
|
|
|
__typeof__(strcat) __strcat_orig __asm__(__USER_LABEL_PREFIX__ "strcat");
|
2015-03-14 09:38:11 +00:00
|
|
|
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
|
2015-03-13 11:00:46 +00:00
|
|
|
char *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();
|
2015-03-13 11:00:46 +00:00
|
|
|
return __strcat_orig(dest, src);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 19:06:48 +00:00
|
|
|
__typeof__(strcpy) __strcpy_orig __asm__(__USER_LABEL_PREFIX__ "strcpy");
|
2015-03-14 09:38:11 +00:00
|
|
|
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
|
2015-03-13 11:00:46 +00:00
|
|
|
char *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();
|
2015-03-13 11:00:46 +00:00
|
|
|
return __strcpy_orig(dest, src);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 19:06:48 +00:00
|
|
|
__typeof__(strncat) __strncat_orig __asm__(__USER_LABEL_PREFIX__ "strncat");
|
2015-03-14 09:38:11 +00:00
|
|
|
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
|
2015-03-13 11:00:46 +00:00
|
|
|
char *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
|
|
|
|
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();
|
|
|
|
}
|
2015-03-13 11:00:46 +00:00
|
|
|
return __strncat_orig(dest, src, n);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 19:06:48 +00:00
|
|
|
__typeof__(strncpy) __strncpy_orig __asm__(__USER_LABEL_PREFIX__ "strncpy");
|
2015-03-14 09:38:11 +00:00
|
|
|
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
|
2015-03-13 11:00:46 +00:00
|
|
|
char *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);
|
|
|
|
|
2015-01-28 17:12:12 +00:00
|
|
|
if (n > bos)
|
2015-01-28 15:36:44 +00:00
|
|
|
__builtin_trap();
|
2015-03-13 11:00:46 +00:00
|
|
|
return __strncpy_orig(dest, src, n);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 17:47:08 +00:00
|
|
|
#ifdef _GNU_SOURCE
|
2015-03-13 11:00:46 +00:00
|
|
|
#undef mempcpy
|
2015-03-14 19:06:48 +00:00
|
|
|
__typeof__(mempcpy) __mempcpy_orig __asm__(__USER_LABEL_PREFIX__ "mempcpy");
|
2015-03-14 09:38:11 +00:00
|
|
|
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
|
2015-03-13 11:00:46 +00:00
|
|
|
void *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();
|
2015-03-13 11:00:46 +00:00
|
|
|
return __mempcpy_orig(dest, src, n);
|
2015-01-28 17:44:38 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-28 15:14:49 +00:00
|
|
|
#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
|
2015-03-13 11:00:46 +00:00
|
|
|
#undef strlcat
|
|
|
|
#undef strlcpy
|
2015-03-14 19:06:48 +00:00
|
|
|
__typeof__(strlcat) __strlcat_orig __asm__(__USER_LABEL_PREFIX__ "strlcat");
|
2015-03-14 09:38:11 +00:00
|
|
|
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
|
2015-03-13 11:00:46 +00:00
|
|
|
size_t 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);
|
|
|
|
|
2015-01-28 17:12:12 +00:00
|
|
|
if (n > bos)
|
2015-01-28 15:36:44 +00:00
|
|
|
__builtin_trap();
|
2015-03-13 11:00:46 +00:00
|
|
|
return __strlcat_orig(dest, src, n);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 19:06:48 +00:00
|
|
|
__typeof__(strlcpy) __strlcpy_orig __asm__(__USER_LABEL_PREFIX__ "strlcpy");
|
2015-03-14 09:38:11 +00:00
|
|
|
extern __inline __attribute__((__always_inline__,__gnu_inline__,__artificial__))
|
2015-03-13 11:00:46 +00:00
|
|
|
size_t 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);
|
|
|
|
|
2015-01-28 17:12:12 +00:00
|
|
|
if (n > bos)
|
2015-01-28 15:36:44 +00:00
|
|
|
__builtin_trap();
|
2015-03-13 11:00:46 +00:00
|
|
|
return __strlcpy_orig(dest, src, n);
|
2015-01-28 15:14:49 +00:00
|
|
|
}
|
2015-01-28 16:07:24 +00:00
|
|
|
#endif
|
|
|
|
|
2015-03-13 23:09:15 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
2015-03-11 09:26:11 +00:00
|
|
|
#endif
|
|
|
|
|
2015-01-28 15:14:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|