Coverage Report

Created: 2023-06-07 06:47

/src/sudo/lib/util/strlcpy.c
Line
Count
Source (jump to first uncovered line)
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
/*
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_STRLCPY
30
31
#include <string.h>
32
33
#include "sudo_compat.h"
34
35
/*
36
 * Copy string src to buffer dst of size dsize.  At most dsize-1
37
 * chars will be copied.  Always NUL terminates (unless dsize == 0).
38
 * Returns strlen(src); if retval >= dsize, truncation occurred.
39
 */
40
size_t
41
sudo_strlcpy(char *dst, const char *src, size_t dsize)
42
9.46M
{
43
9.46M
  const char *osrc = src;
44
9.46M
  size_t nleft = dsize;
45
46
  /* Copy as many bytes as will fit. */
47
9.46M
  if (nleft != 0) {
48
58.2M
    while (--nleft != 0) {
49
58.2M
      if ((*dst++ = *src++) == '\0')
50
9.46M
        break;
51
58.2M
    }
52
9.46M
  }
53
54
  /* Not enough room in dst, add NUL and traverse rest of src. */
55
9.46M
  if (nleft == 0) {
56
929
    if (dsize != 0)
57
929
      *dst = '\0';   /* NUL-terminate dst */
58
929
    while (*src++)
59
0
      continue;
60
929
  }
61
62
9.46M
  return(src - osrc - 1); /* count does not include NUL */
63
9.46M
}
64
#endif /* HAVE_STRLCPY */