wchar.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_WCHAR_H
  16. #define _FORTIFY_WCHAR_H
  17. __extension__
  18. #include_next <limits.h>
  19. __extension__
  20. #include_next <stdlib.h>
  21. __extension__
  22. #include_next <string.h>
  23. __extension__
  24. #include_next <wchar.h>
  25. #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
  26. #include "fortify-headers.h"
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #undef fgetws
  31. #undef mbsrtowcs
  32. #undef mbstowcs
  33. #undef wcrtomb
  34. #undef wcscat
  35. #undef wcscpy
  36. #undef wcsncat
  37. #undef wcsncpy
  38. #undef wcsrtombs
  39. #undef wcstombs
  40. #undef wctomb
  41. #undef wmemcpy
  42. #undef wmemmove
  43. #undef wmemset
  44. _FORTIFY_FN(fgetws) wchar_t *fgetws(wchar_t *__s, int __n, FILE *__f)
  45. {
  46. size_t __b = __builtin_object_size(__s, 0);
  47. if ((size_t)__n > __b / sizeof(wchar_t))
  48. __builtin_trap();
  49. return __orig_fgetws(__s, __n, __f);
  50. }
  51. #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
  52. || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
  53. #undef mbsnrtowcs
  54. _FORTIFY_FN(mbsnrtowcs) size_t mbsnrtowcs(wchar_t *__d, const char **__s, size_t __n,
  55. size_t __wn, mbstate_t *__st)
  56. {
  57. size_t __b = __builtin_object_size(__d, 0);
  58. size_t __r;
  59. if (__wn > __n / sizeof(wchar_t)) {
  60. __b /= sizeof(wchar_t);
  61. __r = __orig_mbsnrtowcs(__d, __s, __n, __wn > __b ? __b : __wn, __st);
  62. if (__b < __wn && __d && *__s && __r != (size_t)-1)
  63. __builtin_trap();
  64. } else {
  65. __r = __orig_mbsnrtowcs(__d, __s, __n > __b ? __b : __n, __wn, __st);
  66. if (__b < __n && __d && *__s && __r != (size_t)-1)
  67. __builtin_trap();
  68. }
  69. return __r;
  70. }
  71. #endif
  72. _FORTIFY_FN(mbsrtowcs) size_t mbsrtowcs(wchar_t *__d, const char **__s, size_t __wn,
  73. mbstate_t *__st)
  74. {
  75. size_t __b = __builtin_object_size(__d, 0);
  76. size_t __r;
  77. __b /= sizeof(wchar_t);
  78. __r = __orig_mbsrtowcs(__d, __s, __wn > __b ? __b : __wn, __st);
  79. if (__b < __wn && __d && *__s && __r != (size_t)-1)
  80. __builtin_trap();
  81. return __r;
  82. }
  83. _FORTIFY_FN(mbstowcs) size_t mbstowcs(wchar_t *__ws, const char *__s, size_t __wn)
  84. {
  85. size_t __b = __builtin_object_size(__ws, 0);
  86. if (__ws && __wn > __b / sizeof(wchar_t))
  87. __builtin_trap();
  88. return __orig_mbstowcs(__ws, __s, __wn);
  89. }
  90. _FORTIFY_FN(wcrtomb) size_t wcrtomb(char *__s, wchar_t __w, mbstate_t *__st)
  91. {
  92. char __buf[MB_LEN_MAX];
  93. size_t __b = __builtin_object_size(__s, 0);
  94. size_t __r;
  95. if (__s) {
  96. __r = __orig_wcrtomb(__buf, __w, __st);
  97. if (__r > __b)
  98. __builtin_trap();
  99. memcpy(__s, __buf, __r);
  100. return __r;
  101. }
  102. return __orig_wcrtomb(__s, __w, __st);
  103. }
  104. _FORTIFY_FN(wcscat) wchar_t *wcscat(wchar_t *__d, const wchar_t *__s)
  105. {
  106. size_t __b = __builtin_object_size(__d, 0);
  107. if (wcslen(__s) + wcslen(__d) + 1 > __b / sizeof(wchar_t))
  108. __builtin_trap();
  109. return __orig_wcscat(__d, __s);
  110. }
  111. _FORTIFY_FN(wcscpy) wchar_t *wcscpy(wchar_t *__d, const wchar_t *__s)
  112. {
  113. size_t __b = __builtin_object_size(__d, 0);
  114. if (wcslen(__s) + 1 > __b / sizeof(wchar_t))
  115. __builtin_trap();
  116. return __orig_wcscpy(__d, __s);
  117. }
  118. _FORTIFY_FN(wcsncat) wchar_t *wcsncat(wchar_t *__d, const wchar_t *__s, size_t __n)
  119. {
  120. size_t __b = __builtin_object_size(__d, 0);
  121. size_t __sl, __dl;
  122. if (__n > __b / sizeof(wchar_t)) {
  123. __sl = wcslen(__s);
  124. __dl = wcslen(__d);
  125. if (__sl > __n)
  126. __sl = __n;
  127. if (__sl + __dl + 1 > __b / sizeof(wchar_t))
  128. __builtin_trap();
  129. }
  130. return __orig_wcsncat(__d, __s, __n);
  131. }
  132. _FORTIFY_FN(wcsncpy) wchar_t *wcsncpy(wchar_t *__d, const wchar_t *__s, size_t __n)
  133. {
  134. size_t __b = __builtin_object_size(__d, 0);
  135. if (__n > __b / sizeof(wchar_t))
  136. __builtin_trap();
  137. return __orig_wcsncpy(__d, __s, __n);
  138. }
  139. #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
  140. || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
  141. #undef wcsnrtombs
  142. _FORTIFY_FN(wcsnrtombs) size_t wcsnrtombs(char *__d, const wchar_t **__s, size_t __wn,
  143. size_t __n, mbstate_t *__st)
  144. {
  145. size_t __b = __builtin_object_size(__d, 0);
  146. size_t __r;
  147. if (__wn > __n / sizeof(wchar_t)) {
  148. __b /= sizeof(wchar_t);
  149. __r = __orig_wcsnrtombs(__d, __s, __wn > __b ? __b : __wn, __n, __st);
  150. if (__b < __wn && __d && *__s && __r != (size_t)-1)
  151. __builtin_trap();
  152. } else {
  153. __r = __orig_wcsnrtombs(__d, __s, __wn, __n > __b ? __b : __n, __st);
  154. if (__b < __n && __d && *__s && __r != (size_t)-1)
  155. __builtin_trap();
  156. }
  157. return __r;
  158. }
  159. #endif
  160. _FORTIFY_FN(wcsrtombs) size_t wcsrtombs(char *__d, const wchar_t **__s, size_t __n,
  161. mbstate_t *__st)
  162. {
  163. size_t __b = __builtin_object_size(__d, 0);
  164. size_t __r;
  165. __r = __orig_wcsrtombs(__d, __s, __n > __b ? __b : __n, __st);
  166. if (__b < __n && __d && *__s && __r != (size_t)-1)
  167. __builtin_trap();
  168. return __r;
  169. }
  170. _FORTIFY_FN(wcstombs) size_t wcstombs(char *__s, const wchar_t *__ws, size_t __n)
  171. {
  172. size_t __b = __builtin_object_size(__s, 0);
  173. if (__s && __n > __b)
  174. __builtin_trap();
  175. return __orig_wcstombs(__s, __ws, __n);
  176. }
  177. _FORTIFY_FN(wctomb) int wctomb(char *__s, wchar_t __w)
  178. {
  179. size_t __b = __builtin_object_size(__s, 0);
  180. if (__s && MB_CUR_MAX > __b)
  181. __builtin_trap();
  182. return __orig_wctomb(__s, __w);
  183. }
  184. _FORTIFY_FN(wmemcpy) wchar_t *wmemcpy(wchar_t *__d, const wchar_t *__s, size_t __n)
  185. {
  186. size_t __b = __builtin_object_size(__d, 0);
  187. if (__n > __b / sizeof(wchar_t))
  188. __builtin_trap();
  189. return __orig_wmemcpy(__d, __s, __n);
  190. }
  191. _FORTIFY_FN(wmemmove) wchar_t *wmemmove(wchar_t *__d, const wchar_t *__s, size_t __n)
  192. {
  193. size_t __b = __builtin_object_size(__d, 0);
  194. if (__n > __b / sizeof(wchar_t))
  195. __builtin_trap();
  196. return __orig_wmemmove(__d, __s, __n);
  197. }
  198. _FORTIFY_FN(wmemset) wchar_t *wmemset(wchar_t *__s, wchar_t __c, size_t __n)
  199. {
  200. size_t __b = __builtin_object_size(__s, 0);
  201. if (__n > __b / sizeof(wchar_t))
  202. __builtin_trap();
  203. return __orig_wmemset(__s, __c, __n);
  204. }
  205. #ifdef __cplusplus
  206. }
  207. #endif
  208. #endif
  209. #endif