stdlib.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* Checking macros for stdlib functions.
  2. Copyright (C) 2005-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _STDLIB_H
  16. # error "Never include <bits/stdlib.h> directly; use <stdlib.h> instead."
  17. #endif
  18. extern char *__realpath_chk (const char *__restrict __name,
  19. char *__restrict __resolved,
  20. size_t __resolvedlen) __THROW __wur;
  21. extern char *__REDIRECT_NTH (__realpath_alias,
  22. (const char *__restrict __name,
  23. char *__restrict __resolved), realpath) __wur;
  24. extern char *__REDIRECT_NTH (__realpath_chk_warn,
  25. (const char *__restrict __name,
  26. char *__restrict __resolved,
  27. size_t __resolvedlen), __realpath_chk) __wur
  28. __warnattr ("second argument of realpath must be either NULL or at "
  29. "least PATH_MAX bytes long buffer");
  30. __fortify_function __attribute_overloadable__ __wur char *
  31. __NTH (realpath (const char *__restrict __name,
  32. __fortify_clang_overload_arg (char *, __restrict, __resolved)))
  33. #if defined _LIBC_LIMITS_H_ && defined PATH_MAX
  34. __fortify_clang_warning_only_if_bos_lt (PATH_MAX, __resolved,
  35. "second argument of realpath must be "
  36. "either NULL or at least PATH_MAX "
  37. "bytes long buffer")
  38. #endif
  39. {
  40. size_t __sz = __glibc_objsize (__resolved);
  41. if (__sz == (size_t) -1)
  42. return __realpath_alias (__name, __resolved);
  43. #if !__fortify_use_clang && defined _LIBC_LIMITS_H_ && defined PATH_MAX
  44. if (__glibc_unsafe_len (PATH_MAX, sizeof (char), __sz))
  45. return __realpath_chk_warn (__name, __resolved, __sz);
  46. #endif
  47. return __realpath_chk (__name, __resolved, __sz);
  48. }
  49. extern int __ptsname_r_chk (int __fd, char *__buf, size_t __buflen,
  50. size_t __nreal) __THROW __nonnull ((2))
  51. __attr_access ((__write_only__, 2, 3));
  52. extern int __REDIRECT_NTH (__ptsname_r_alias, (int __fd, char *__buf,
  53. size_t __buflen), ptsname_r)
  54. __nonnull ((2)) __attr_access ((__write_only__, 2, 3));
  55. extern int __REDIRECT_NTH (__ptsname_r_chk_warn,
  56. (int __fd, char *__buf, size_t __buflen,
  57. size_t __nreal), __ptsname_r_chk)
  58. __nonnull ((2)) __warnattr ("ptsname_r called with buflen bigger than "
  59. "size of buf");
  60. __fortify_function __attribute_overloadable__ int
  61. __NTH (ptsname_r (int __fd,
  62. __fortify_clang_overload_arg (char *, ,__buf),
  63. size_t __buflen))
  64. __fortify_clang_warning_only_if_bos_lt (__buflen, __buf,
  65. "ptsname_r called with buflen "
  66. "bigger than size of buf")
  67. {
  68. return __glibc_fortify (ptsname_r, __buflen, sizeof (char),
  69. __glibc_objsize (__buf),
  70. __fd, __buf, __buflen);
  71. }
  72. extern int __wctomb_chk (char *__s, wchar_t __wchar, size_t __buflen)
  73. __THROW __wur;
  74. extern int __REDIRECT_NTH (__wctomb_alias, (char *__s, wchar_t __wchar),
  75. wctomb) __wur;
  76. __fortify_function __attribute_overloadable__ __wur int
  77. __NTH (wctomb (__fortify_clang_overload_arg (char *, ,__s), wchar_t __wchar))
  78. {
  79. /* We would have to include <limits.h> to get a definition of MB_LEN_MAX.
  80. But this would only disturb the namespace. So we define our own
  81. version here. */
  82. #define __STDLIB_MB_LEN_MAX 16
  83. #if defined MB_LEN_MAX && MB_LEN_MAX != __STDLIB_MB_LEN_MAX
  84. # error "Assumed value of MB_LEN_MAX wrong"
  85. #endif
  86. if (__glibc_objsize (__s) != (size_t) -1
  87. && __STDLIB_MB_LEN_MAX > __glibc_objsize (__s))
  88. return __wctomb_chk (__s, __wchar, __glibc_objsize (__s));
  89. return __wctomb_alias (__s, __wchar);
  90. }
  91. extern size_t __mbstowcs_chk (wchar_t *__restrict __dst,
  92. const char *__restrict __src,
  93. size_t __len, size_t __dstlen) __THROW
  94. __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2));
  95. extern size_t __REDIRECT_NTH (__mbstowcs_nulldst,
  96. (wchar_t *__restrict __dst,
  97. const char *__restrict __src,
  98. size_t __len), mbstowcs)
  99. __attr_access ((__read_only__, 2));
  100. extern size_t __REDIRECT_NTH (__mbstowcs_alias,
  101. (wchar_t *__restrict __dst,
  102. const char *__restrict __src,
  103. size_t __len), mbstowcs)
  104. __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2));
  105. extern size_t __REDIRECT_NTH (__mbstowcs_chk_warn,
  106. (wchar_t *__restrict __dst,
  107. const char *__restrict __src,
  108. size_t __len, size_t __dstlen), __mbstowcs_chk)
  109. __warnattr ("mbstowcs called with dst buffer smaller than len "
  110. "* sizeof (wchar_t)");
  111. __fortify_function __attribute_overloadable__ size_t
  112. __NTH (mbstowcs (__fortify_clang_overload_arg (wchar_t *, __restrict, __dst),
  113. const char *__restrict __src,
  114. size_t __len))
  115. __fortify_clang_warning_only_if_bos0_lt2 (__len, __dst, sizeof (wchar_t),
  116. "mbstowcs called with dst buffer "
  117. "smaller than len * sizeof (wchar_t)")
  118. {
  119. if (__builtin_constant_p (__dst == NULL) && __dst == NULL)
  120. return __mbstowcs_nulldst (__dst, __src, __len);
  121. else
  122. return __glibc_fortify_n (mbstowcs, __len, sizeof (wchar_t),
  123. __glibc_objsize (__dst), __dst, __src, __len);
  124. }
  125. extern size_t __wcstombs_chk (char *__restrict __dst,
  126. const wchar_t *__restrict __src,
  127. size_t __len, size_t __dstlen) __THROW
  128. __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2));
  129. extern size_t __REDIRECT_NTH (__wcstombs_alias,
  130. (char *__restrict __dst,
  131. const wchar_t *__restrict __src,
  132. size_t __len), wcstombs)
  133. __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2));
  134. extern size_t __REDIRECT_NTH (__wcstombs_chk_warn,
  135. (char *__restrict __dst,
  136. const wchar_t *__restrict __src,
  137. size_t __len, size_t __dstlen), __wcstombs_chk)
  138. __warnattr ("wcstombs called with dst buffer smaller than len");
  139. __fortify_function __attribute_overloadable__ size_t
  140. __NTH (wcstombs (__fortify_clang_overload_arg (char *, __restrict, __dst),
  141. const wchar_t *__restrict __src,
  142. size_t __len))
  143. {
  144. return __glibc_fortify (wcstombs, __len, sizeof (char),
  145. __glibc_objsize (__dst),
  146. __dst, __src, __len);
  147. }