stdlib.h 7.1 KB

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