stdio.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_STDIO_H
  16. #define _FORTIFY_STDIO_H
  17. __extension__
  18. #include_next <stdio.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 fgets
  25. #undef fread
  26. #undef fwrite
  27. #undef vsprintf
  28. #undef vsnprintf
  29. #undef snprintf
  30. #undef sprintf
  31. _FORTIFY_FN(fgets) char *fgets(char *__s, int __n, FILE *__f)
  32. {
  33. size_t __b = __builtin_object_size(__s, 0);
  34. if ((size_t)__n > __b)
  35. __builtin_trap();
  36. return __orig_fgets(__s, __n, __f);
  37. }
  38. _FORTIFY_FN(fread) size_t fread(void *__d, size_t __n, size_t __m, FILE *__f)
  39. {
  40. size_t __b = __builtin_object_size(__d, 0);
  41. if (__n != 0 && (__n * __m) / __n != __m)
  42. __builtin_trap();
  43. if (__n * __m > __b)
  44. __builtin_trap();
  45. return __orig_fread(__d, __n, __m, __f);
  46. }
  47. _FORTIFY_FN(fwrite) size_t fwrite(const void *__d, size_t __n, size_t __m, FILE *__f)
  48. {
  49. size_t __b = __builtin_object_size(__d, 0);
  50. if (__n != 0 && (__n * __m) / __n != __m)
  51. __builtin_trap();
  52. if (__n * __m > __b)
  53. __builtin_trap();
  54. return __orig_fwrite(__d, __n, __m, __f);
  55. }
  56. _FORTIFY_FN(vsnprintf) int vsnprintf(char *__s, size_t __n, const char *__f,
  57. __builtin_va_list __v)
  58. {
  59. size_t __b = __builtin_object_size(__s, 0);
  60. if (__n > __b)
  61. __builtin_trap();
  62. return __orig_vsnprintf(__s, __n, __f, __v);
  63. }
  64. _FORTIFY_FN(vsprintf) int vsprintf(char *__s, const char *__f, __builtin_va_list __v)
  65. {
  66. size_t __b = __builtin_object_size(__s, 0);
  67. int __r;
  68. if (__b != (size_t)-1) {
  69. __r = __orig_vsnprintf(__s, __b, __f, __v);
  70. if (__r != -1 && (size_t)__r >= __b)
  71. __builtin_trap();
  72. } else {
  73. __r = __orig_vsprintf(__s, __f, __v);
  74. }
  75. return __r;
  76. }
  77. _FORTIFY_FN(snprintf) int snprintf(char *__s, size_t __n, const char *__f, ...)
  78. {
  79. size_t __b = __builtin_object_size(__s, 0);
  80. if (__n > __b)
  81. __builtin_trap();
  82. return __orig_snprintf(__s, __n, __f, __builtin_va_arg_pack());
  83. }
  84. _FORTIFY_FN(sprintf) int sprintf(char *__s, const char *__f, ...)
  85. {
  86. size_t __b = __builtin_object_size(__s, 0);
  87. int __r;
  88. if (__b != (size_t)-1) {
  89. __r = __orig_snprintf(__s, __b, __f, __builtin_va_arg_pack());
  90. if (__r != -1 && (size_t)__r >= __b)
  91. __builtin_trap();
  92. } else {
  93. __r = __orig_sprintf(__s, __f, __builtin_va_arg_pack());
  94. }
  95. return __r;
  96. }
  97. #ifdef __cplusplus
  98. }
  99. #endif
  100. #endif
  101. #endif