Coverage Report

Created: 2022-07-02 04:24

/src/php-src/main/strlcat.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
  +----------------------------------------------------------------------+
3
  | Copyright (c) The PHP Group                                          |
4
  +----------------------------------------------------------------------+
5
  | This source file is subject to version 3.01 of the PHP license,      |
6
  | that is bundled with this package in the file LICENSE, and is        |
7
  | available through the world-wide-web at the following url:           |
8
  | http://www.php.net/license/3_01.txt                                  |
9
  | If you did not receive a copy of the PHP license and are unable to   |
10
  | obtain it through the world-wide-web, please send a note to          |
11
  | license@php.net so we can mail you a copy immediately.               |
12
  +----------------------------------------------------------------------+
13
  | Author:                                                              |
14
  +----------------------------------------------------------------------+
15
*/
16
17
#include "php.h"
18
19
#ifdef USE_STRLCAT_PHP_IMPL
20
21
/*     $OpenBSD: strlcat.c,v 1.18 2016/10/16 17:37:39 dtucker Exp $    */
22
23
/*
24
 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
25
 * All rights reserved.
26
 *
27
 * Redistribution and use in source and binary forms, with or without
28
 * modification, are permitted provided that the following conditions
29
 * are met:
30
 * 1. Redistributions of source code must retain the above copyright
31
 *    notice, this list of conditions and the following disclaimer.
32
 * 2. Redistributions in binary form must reproduce the above copyright
33
 *    notice, this list of conditions and the following disclaimer in the
34
 *    documentation and/or other materials provided with the distribution.
35
 * 3. The name of the author may not be used to endorse or promote products
36
 *    derived from this software without specific prior written permission.
37
 *
38
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
39
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
40
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
41
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
44
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
45
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
46
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
47
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48
 */
49
50
#if defined(LIBC_SCCS) && !defined(lint)
51
static char *rcsid = "$OpenBSD: strlcat.c,v 1.17 2016/10/14 18:19:04 dtucker Exp $";
52
#endif /* LIBC_SCCS and not lint */
53
54
#include <sys/types.h>
55
#include <string.h>
56
57
/*
58
 * Appends src to string dst of size siz (unlike strncat, siz is the
59
 * full size of dst, not space left).  At most siz-1 characters
60
 * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
61
 * Returns strlen(src) + MIN(siz, strlen(initial dst).
62
 * If retval >= siz, truncation occurred.
63
 */
64
PHPAPI size_t php_strlcat(dst, src, siz)
65
  char *dst;
66
  const char *src;
67
  size_t siz;
68
11.0k
{
69
11.0k
  const char *d = dst;
70
11.0k
  const char *s = src;
71
11.0k
  size_t n = siz;
72
11.0k
  size_t dlen;
73
74
  /* Find the end of dst and adjust bytes left but don't go past end */
75
22.0k
  while (n-- != 0 && *dst != '\0')
76
11.0k
    dst++;
77
11.0k
  dlen = dst - d;
78
11.0k
  n = siz - dlen;
79
80
11.0k
  if (n-- == 0)
81
0
    return(dlen + strlen(src));
82
69.9k
  while (*src != '\0') {
83
58.8k
    if (n != 0) {
84
58.8k
      *dst++ = *src;
85
58.8k
      n--;
86
58.8k
    }
87
58.8k
    src++;
88
58.8k
  }
89
11.0k
  *dst = '\0';
90
91
11.0k
  return(dlen + (src - s));
92
11.0k
}
93
94
#endif /* !HAVE_STRLCAT */