/src/sudo/lib/util/strlcat.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: strlcat.c,v 1.15 2015/03/02 21:41:08 millert Exp $ */ |
2 | | |
3 | | /* |
4 | | * SPDX-License-Identifier: ISC |
5 | | * |
6 | | * Copyright (c) 1998, 2003-2005, 2010-2011, 2013-2015 |
7 | | * Todd C. Miller <Todd.Miller@sudo.ws> |
8 | | * |
9 | | * Permission to use, copy, modify, and distribute this software for any |
10 | | * purpose with or without fee is hereby granted, provided that the above |
11 | | * copyright notice and this permission notice appear in all copies. |
12 | | * |
13 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
14 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
15 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
16 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
17 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
18 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
19 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
20 | | */ |
21 | | |
22 | | /* |
23 | | * This is an open source non-commercial project. Dear PVS-Studio, please check it. |
24 | | * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
25 | | */ |
26 | | |
27 | | #include <config.h> |
28 | | |
29 | | #ifndef HAVE_STRLCAT |
30 | | |
31 | | #include <string.h> |
32 | | |
33 | | #include "sudo_compat.h" |
34 | | |
35 | | /* |
36 | | * Appends src to string dst of size dsize (unlike strncat, dsize is the |
37 | | * full size of dst, not space left). At most dsize-1 characters |
38 | | * will be copied. Always NUL terminates (unless dsize <= strlen(dst)). |
39 | | * Returns strlen(src) + MIN(dsize, strlen(initial dst)). |
40 | | * If retval >= dsize, truncation occurred. |
41 | | */ |
42 | | size_t |
43 | | sudo_strlcat(char *dst, const char *src, size_t dsize) |
44 | 419k | { |
45 | 419k | const char *odst = dst; |
46 | 419k | const char *osrc = src; |
47 | 419k | size_t n = dsize; |
48 | 419k | size_t dlen; |
49 | | |
50 | | /* Find the end of dst and adjust bytes left but don't go past end. */ |
51 | 3.60M | while (n-- != 0 && *dst != '\0') |
52 | 3.18M | dst++; |
53 | 419k | dlen = dst - odst; |
54 | 419k | n = dsize - dlen; |
55 | | |
56 | 419k | if (n-- == 0) |
57 | 0 | return(dlen + strlen(src)); |
58 | 51.9M | while (*src != '\0') { |
59 | 51.5M | if (n != 0) { |
60 | 51.5M | *dst++ = *src; |
61 | 51.5M | n--; |
62 | 51.5M | } |
63 | 51.5M | src++; |
64 | 51.5M | } |
65 | 419k | *dst = '\0'; |
66 | | |
67 | 419k | return(dlen + (src - osrc)); /* count does not include NUL */ |
68 | 419k | } |
69 | | #endif /* HAVE_STRLCAT */ |