stdlib.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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_STDLIB_H
  16. #define _FORTIFY_STDLIB_H
  17. __extension__
  18. #include_next <stdlib.h>
  19. #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
  20. __extension__
  21. #include_next <limits.h>
  22. #endif
  23. #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
  24. #include "fortify-headers.h"
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
  29. #undef realpath
  30. _FORTIFY_FN(realpath) char *realpath(const char *__p, char *__r)
  31. {
  32. size_t __b = __builtin_object_size(__r, 0);
  33. if (__r) {
  34. #ifndef PATH_MAX
  35. #error PATH_MAX unset. A fortified realpath will not work.
  36. #else
  37. char __buf[PATH_MAX], *__ret;
  38. size_t __l;
  39. __ret = __orig_realpath(__p, __buf);
  40. if (!__ret)
  41. return NULL;
  42. __l = __builtin_strlen(__ret) + 1;
  43. if (__l > __b)
  44. __builtin_trap();
  45. __builtin_memcpy(__r, __ret, __l);
  46. return __r;
  47. #endif
  48. }
  49. return __orig_realpath(__p, __r);
  50. }
  51. #endif
  52. #ifdef __cplusplus
  53. }
  54. #endif
  55. #endif
  56. #endif