memset_explicit_chk.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* Generic implementation of __memset_explicit_chk.
  2. Copyright (C) 1991-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. /* This is the generic definition of __memset_explicit_chk. The
  16. __memset_explicit_chk symbol is used as the implementation of
  17. memset_explicit throughout glibc. If this file is overridden by an
  18. architecture, both __memset_explicit_chk and
  19. __memset_explicit_chk_internal have to be defined (the latter not as
  20. an IFUNC). */
  21. #include <string.h>
  22. void *
  23. __memset_explicit_chk (void *dst, int c, size_t len, size_t dstlen)
  24. {
  25. /* Inline __memset_chk to avoid a PLT reference to __memset_chk. */
  26. if (__glibc_unlikely (dstlen < len))
  27. __chk_fail ();
  28. memset (dst, c, len);
  29. /* Compiler barrier. */
  30. asm volatile ("" ::: "memory");
  31. return dst;
  32. }
  33. /* libc-internal references use the hidden
  34. __memset_explicit_chk_internal symbol. This is necessary if
  35. __memset_explicit_chk is implemented as an IFUNC because some
  36. targets do not support hidden references to IFUNC symbols. */
  37. strong_alias (__memset_explicit_chk, __memset_explicit_chk_internal)