1238104Sdes/*-
2238104Sdes *
3238104Sdes * SPDX-License-Identifier: BSD-2-Clause
4238104Sdes *
5238104Sdes * Copyright (c) 2006 The NetBSD Foundation, Inc.
6238104Sdes * All rights reserved.
7238104Sdes *
8238104Sdes * This code is derived from software contributed to The NetBSD Foundation
9238104Sdes * by Christos Zoulas.
10238104Sdes *
11238104Sdes * Redistribution and use in source and binary forms, with or without
12238104Sdes * modification, are permitted provided that the following conditions
13238104Sdes * are met:
14238104Sdes * 1. Redistributions of source code must retain the above copyright
15238104Sdes *    notice, this list of conditions and the following disclaimer.
16238104Sdes * 2. Redistributions in binary form must reproduce the above copyright
17238104Sdes *    notice, this list of conditions and the following disclaimer in the
18238104Sdes *    documentation and/or other materials provided with the distribution.
19238104Sdes *
20238104Sdes * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21238104Sdes * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22238104Sdes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23238104Sdes * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24238104Sdes * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25238104Sdes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26238104Sdes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27238104Sdes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28238104Sdes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29238104Sdes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30238104Sdes * POSSIBILITY OF SUCH DAMAGE.
31238104Sdes */
32238104Sdes#include <sys/cdefs.h>
33238104Sdes__RCSID("$NetBSD: strncat_chk.c,v 1.5 2014/09/17 00:39:28 joerg Exp $");
34238104Sdes
35238104Sdes#include <stdio.h>
36238104Sdes#include <string.h>
37238104Sdes
38238104Sdes#include <ssp/string.h>
39238104Sdes
40238104Sdeschar *
41238104Sdes__strncat_chk(char * __restrict dst, const char * __restrict src, size_t len,
42238104Sdes    size_t slen)
43238104Sdes{
44238104Sdes	char *d;
45238104Sdes
46238104Sdes	if (len == 0)
47238104Sdes		return (dst);
48238104Sdes
49238104Sdes	if (len > slen)
50238104Sdes		__chk_fail();
51238104Sdes
52238104Sdes	for (d = dst; *d; d++) {
53238104Sdes		if (slen-- == 0)
54238104Sdes			__chk_fail();
55238104Sdes	}
56238104Sdes
57238104Sdes	do {
58238104Sdes		if ((*d = *src++) == '\0')
59238104Sdes			break;
60238104Sdes		if (slen-- == 0)
61238104Sdes			__chk_fail();
62238104Sdes		d++;
63238104Sdes	} while (--len != 0);
64238104Sdes
65238104Sdes	if (slen-- == 0)
66238104Sdes		__chk_fail();
67238104Sdes
68238104Sdes	*d = '\0';
69238104Sdes	return (dst);
70238104Sdes}
71238104Sdes