xref: /freebsd/lib/libc/secure/strlcat_chk.c (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
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, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include <ssp/string.h>
25 
26 /*
27  * Appends src to string dst of size dsize (unlike strncat, dsize is the
28  * full size of dst, not space left).  At most dsize-1 characters
29  * will be copied.  Always NUL terminates (unless dsize <= strlen(dst)).
30  * Returns strlen(src) + MIN(dsize, strlen(initial dst)).
31  * If retval >= dsize, truncation occurred.
32  */
33 size_t
34 __strlcat_chk(char * __restrict dst, const char * __restrict src, size_t dsize,
35     size_t dbufsize)
36 {
37 	const char *odst = dst;
38 	const char *osrc = src;
39 	size_t n = dsize;
40 	size_t dlen;
41 
42 	if (dsize > dbufsize)
43 		__chk_fail();
44 
45 	/* Find the end of dst and adjust bytes left but don't go past end. */
46 	while (n-- != 0 && *dst != '\0') {
47 		dst++;
48 	}
49 
50 	dlen = dst - odst;
51 	n = dsize - dlen;
52 
53 	if (n-- == 0)
54 		return (dlen + strlen(src));
55 	while (*src != '\0') {
56 		if (n != 0) {
57 			if (dbufsize-- == 0)
58 				__chk_fail();
59 			*dst++ = *src;
60 			n--;
61 		}
62 
63 		src++;
64 	}
65 
66 	if (dbufsize-- == 0)
67 		__chk_fail();
68 	*dst = '\0';
69 	return (dlen + (src - osrc));	/* count does not include NUL */
70 }
71