]>
git.proxmox.com Git - mirror_frr.git/blob - lib/strlcat.c
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /* Append a null-terminated string to another string, with length checking.
3 * Copyright (C) 2016 Free Software Foundation, Inc.
4 * This file is part of the GNU C Library.
7 /* adapted for Quagga from glibc patch submission originally from
8 * Florian Weimer <fweimer@redhat.com>, 2016-05-18 */
20 size_t strlcat(char *__restrict dest
,
21 const char *__restrict src
, size_t destsize
);
23 size_t strlcat(char *__restrict dest
,
24 const char *__restrict src
, size_t destsize
)
26 size_t src_length
= strlen(src
);
28 /* Our implementation strlcat supports dest == NULL if size == 0
29 (for consistency with snprintf and strlcpy), but strnlen does
30 not, so we have to cover this case explicitly. */
34 size_t dest_length
= strnlen(dest
, destsize
);
35 if (dest_length
!= destsize
) {
36 /* Copy at most the remaining number of characters in the
37 destination buffer. Leave for the NUL terminator. */
38 size_t to_copy
= destsize
- dest_length
- 1;
39 /* But not more than what is available in the source string. */
40 if (to_copy
> src_length
)
43 char *target
= dest
+ dest_length
;
44 memcpy(target
, src
, to_copy
);
45 target
[to_copy
] = '\0';
48 /* If the sum wraps around, we have more than SIZE_MAX + 2 bytes in
49 the two input strings (including both null terminators). If each
50 byte in the address space can be assigned a unique size_t value
51 (which the static_assert checks), then by the pigeonhole
52 principle, the two input strings must overlap, which is
54 _Static_assert(sizeof(uintptr_t) == sizeof(size_t),
55 "theoretical maximum object size covers address space");
56 return dest_length
+ src_length
;
58 #endif /* HAVE_STRLCAT */