rawmemchr.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <string.h>
  15. #include <libc-diag.h>
  16. #ifndef RAWMEMCHR
  17. # define RAWMEMCHR __rawmemchr
  18. #endif
  19. /* The pragmata should be nested inside RAWMEMCHR below, but that
  20. triggers GCC PR 98512. */
  21. DIAG_PUSH_NEEDS_COMMENT;
  22. #if __GNUC_PREREQ (7, 0)
  23. /* GCC 8 warns about the size passed to memchr being larger than
  24. PTRDIFF_MAX; the use of SIZE_MAX is deliberate here. */
  25. DIAG_IGNORE_NEEDS_COMMENT (8, "-Wstringop-overflow=");
  26. #endif
  27. #if __GNUC_PREREQ (11, 0)
  28. /* Likewise GCC 11, with a different warning option. */
  29. DIAG_IGNORE_NEEDS_COMMENT (11, "-Wstringop-overread");
  30. #endif
  31. /* Find the first occurrence of C in S. */
  32. void *
  33. RAWMEMCHR (const void *s, int c)
  34. {
  35. if ((unsigned char) c != '\0')
  36. return memchr (s, c, (size_t)-1);
  37. return (char *)s + strlen (s);
  38. }
  39. libc_hidden_def (__rawmemchr)
  40. weak_alias (__rawmemchr, rawmemchr)
  41. DIAG_POP_NEEDS_COMMENT;