wcslcat.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* Append a null-terminated wide string to another, with length checking.
  2. Copyright (C) 2023-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. #include <stdint.h>
  16. #include <wchar.h>
  17. size_t
  18. __wcslcat (wchar_t *__restrict dest, const wchar_t *__restrict src,
  19. size_t size)
  20. {
  21. size_t src_length = __wcslen (src);
  22. /* Our implementation strlcat supports dest == NULL if size == 0
  23. (for consistency with snprintf and strlcpy), but wcsnlen does
  24. not, so we have to cover this case explicitly. */
  25. if (size == 0)
  26. return src_length;
  27. size_t dest_length = __wcsnlen (dest, size);
  28. if (dest_length != size)
  29. {
  30. /* Copy at most the remaining number of characters in the
  31. destination buffer. Leave for the null terminator. */
  32. size_t to_copy = size - dest_length - 1;
  33. /* But not more than what is available in the source string. */
  34. if (to_copy > src_length)
  35. to_copy = src_length;
  36. wchar_t *target = dest + dest_length;
  37. __wmemcpy (target, src, to_copy);
  38. target[to_copy] = '\0';
  39. }
  40. /* If the sum wraps around, we have more than SIZE_MAX + 2 bytes in
  41. the two input strings (including both null terminators). If each
  42. byte in the address space can be assigned a unique size_t value
  43. (which the static_assert checks), then by the pigeonhole
  44. principle, the two input strings must overlap, which is
  45. undefined. */
  46. _Static_assert (sizeof (uintptr_t) == sizeof (size_t),
  47. "theoretical maximum object size covers address space");
  48. return dest_length + src_length;
  49. }
  50. libc_hidden_def (__wcslcat)
  51. weak_alias (__wcslcat, wcslcat)