Coverage Report

Created: 2025-07-11 06:57

/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 * restrict dst, const char * restrict src, size_t dsize)
44
490k
{
45
490k
  const char *odst = dst;
46
490k
  const char *osrc = src;
47
490k
  size_t n = dsize;
48
490k
  size_t dlen;
49
50
  /* Find the end of dst and adjust bytes left but don't go past end. */
51
4.32M
  while (n-- != 0 && *dst != '\0')
52
3.83M
    dst++;
53
490k
  dlen = (size_t)(dst - odst);
54
490k
  n = dsize - dlen;
55
56
490k
  if (n-- == 0)
57
0
    return(dlen + strlen(src));
58
94.5M
  while (*src != '\0') {
59
94.1M
    if (n != 0) {
60
94.1M
      *dst++ = *src;
61
94.1M
      n--;
62
94.1M
    }
63
94.1M
    src++;
64
94.1M
  }
65
490k
  *dst = '\0';
66
67
490k
  return(dlen + (size_t)(src - osrc));  /* count does not include NUL */
68
490k
}
69
#endif /* HAVE_STRLCAT */