string.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) 2015 Dimitris Papastamos <sin@2f30.org>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #ifndef _FORTIFY_STRING_H
  16. #define _FORTIFY_STRING_H
  17. __extension__
  18. #include_next <string.h>
  19. #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
  20. #include "fortify-headers.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #undef memcpy
  25. #undef memmove
  26. #undef memset
  27. #undef strcat
  28. #undef strcpy
  29. #undef strncat
  30. #undef strncpy
  31. _FORTIFY_FN(memcpy) void *memcpy(void *__od, const void *__os, size_t __n)
  32. {
  33. size_t __bd = __builtin_object_size(__od, 0);
  34. size_t __bs = __builtin_object_size(__os, 0);
  35. char *__d = (char *)__od;
  36. const char *__s = (const char *)__os;
  37. /* trap if pointers are overlapping but not if dst == src.
  38. * gcc seems to like to generate code that relies on dst == src */
  39. if ((__d < __s && __d + __n > __s) ||
  40. (__s < __d && __s + __n > __d))
  41. __builtin_trap();
  42. if (__n > __bd || __n > __bs)
  43. __builtin_trap();
  44. return __orig_memcpy(__od, __os, __n);
  45. }
  46. _FORTIFY_FN(memmove) void *memmove(void *__d, const void *__s, size_t __n)
  47. {
  48. size_t __bd = __builtin_object_size(__d, 0);
  49. size_t __bs = __builtin_object_size(__s, 0);
  50. if (__n > __bd || __n > __bs)
  51. __builtin_trap();
  52. return __orig_memmove(__d, __s, __n);
  53. }
  54. _FORTIFY_FN(memset) void *memset(void *__d, int __c, size_t __n)
  55. {
  56. size_t __b = __builtin_object_size(__d, 0);
  57. if (__n > __b)
  58. __builtin_trap();
  59. return __orig_memset(__d, __c, __n);
  60. }
  61. #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
  62. || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
  63. || defined(_BSD_SOURCE)
  64. #undef stpcpy
  65. _FORTIFY_FN(stpcpy) char *stpcpy(char *__d, const char *__s)
  66. {
  67. size_t __b = __builtin_object_size(__d, 0);
  68. if (strlen(__s) + 1 > __b)
  69. __builtin_trap();
  70. return __orig_stpcpy(__d, __s);
  71. }
  72. #undef stpncpy
  73. _FORTIFY_FN(stpncpy) char *stpncpy(char *__d, const char *__s, size_t __n)
  74. {
  75. size_t __b = __builtin_object_size(__d, 0);
  76. if (__n > __b && strlen(__s) + 1 > __b)
  77. __builtin_trap();
  78. return __orig_stpncpy(__d, __s, __n);
  79. }
  80. #endif
  81. _FORTIFY_FN(strcat) char *strcat(char *__d, const char *__s)
  82. {
  83. size_t __b = __builtin_object_size(__d, 0);
  84. if (strlen(__s) + strlen(__d) + 1 > __b)
  85. __builtin_trap();
  86. return __orig_strcat(__d, __s);
  87. }
  88. _FORTIFY_FN(strcpy) char *strcpy(char *__d, const char *__s)
  89. {
  90. size_t __b = __builtin_object_size(__d, 0);
  91. if (strlen(__s) + 1 > __b)
  92. __builtin_trap();
  93. return __orig_strcpy(__d, __s);
  94. }
  95. _FORTIFY_FN(strncat) char *strncat(char *__d, const char *__s, size_t __n)
  96. {
  97. size_t __b = __builtin_object_size(__d, 0);
  98. size_t __sl, __dl;
  99. if (__n > __b) {
  100. __sl = strlen(__s);
  101. __dl = strlen(__d);
  102. if (__sl > __n)
  103. __sl = __n;
  104. if (__sl + __dl + 1 > __b)
  105. __builtin_trap();
  106. }
  107. return __orig_strncat(__d, __s, __n);
  108. }
  109. _FORTIFY_FN(strncpy) char *strncpy(char *__d, const char *__s, size_t __n)
  110. {
  111. size_t __b = __builtin_object_size(__d, 0);
  112. if (__n > __b)
  113. __builtin_trap();
  114. return __orig_strncpy(__d, __s, __n);
  115. }
  116. #ifdef _GNU_SOURCE
  117. #undef mempcpy
  118. _FORTIFY_FN(mempcpy) void *mempcpy(void *__d, const void *__s, size_t __n)
  119. {
  120. size_t __bd = __builtin_object_size(__d, 0);
  121. size_t __bs = __builtin_object_size(__s, 0);
  122. if (__n > __bd || __n > __bs)
  123. __builtin_trap();
  124. return __orig_mempcpy(__d, __s, __n);
  125. }
  126. #endif
  127. #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
  128. #undef strlcat
  129. #undef strlcpy
  130. _FORTIFY_FN(strlcat) size_t strlcat(char *__d, const char *__s, size_t __n)
  131. {
  132. size_t __b = __builtin_object_size(__d, 0);
  133. if (__n > __b)
  134. __builtin_trap();
  135. return __orig_strlcat(__d, __s, __n);
  136. }
  137. _FORTIFY_FN(strlcpy) size_t strlcpy(char *__d, const char *__s, size_t __n)
  138. {
  139. size_t __b = __builtin_object_size(__d, 0);
  140. if (__n > __b)
  141. __builtin_trap();
  142. return __orig_strlcpy(__d, __s, __n);
  143. }
  144. #endif
  145. #ifdef __cplusplus
  146. }
  147. #endif
  148. #endif
  149. #endif