Coverage Report

Created: 2025-10-10 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/lib/util/strlcpy.c
Line
Count
Source
1
/*  $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 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
#include <config.h>
23
24
#ifndef HAVE_STRLCPY
25
26
#include <string.h>
27
28
#include <sudo_compat.h>
29
30
/*
31
 * Copy string src to buffer dst of size dsize.  At most dsize-1
32
 * chars will be copied.  Always NUL terminates (unless dsize == 0).
33
 * Returns strlen(src); if retval >= dsize, truncation occurred.
34
 */
35
size_t
36
sudo_strlcpy(char * restrict dst, const char * restrict src, size_t dsize)
37
1.90k
{
38
1.90k
  const char *osrc = src;
39
1.90k
  size_t nleft = dsize;
40
41
  /* Copy as many bytes as will fit. */
42
1.90k
  if (nleft != 0) {
43
41.8k
    while (--nleft != 0) {
44
41.8k
      if ((*dst++ = *src++) == '\0')
45
1.90k
        break;
46
41.8k
    }
47
1.90k
  }
48
49
  /* Not enough room in dst, add NUL and traverse rest of src. */
50
1.90k
  if (nleft == 0) {
51
0
    if (dsize != 0)
52
0
      *dst = '\0';   /* NUL-terminate dst */
53
0
    while (*src++)
54
0
      continue;
55
0
  }
56
57
1.90k
  return((size_t)(src - osrc) - 1); /* count does not include NUL */
58
1.90k
}
59
#endif /* HAVE_STRLCPY */