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