Coverage Report

Created: 2026-08-31 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ntp-dev/libntp/strl_obsd.c
Line
Count
Source
1
/* 
2
 * Why use strlcpy()/strlcat() instead of standard strncpy()/strncat()?
3
 * To reduce likelihood of bugs and avoid wasteful zero fills.  See:
4
 * http://www.gratisoft.us/todd/papers/strlcpy.html
5
 */
6
7
/*  $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $  */
8
9
/*
10
 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
11
 *
12
 * Permission to use, copy, modify, and distribute this software for any
13
 * purpose with or without fee is hereby granted, provided that the above
14
 * copyright notice and this permission notice appear in all copies.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23
 */
24
25
#include <config.h>   /* + marks local changes */
26
#ifdef HAVE_SYS_TYPES_H   /* + */
27
#include <sys/types.h>
28
#endif        /* + */
29
#include <string.h>
30
31
#include "ntp_stdlib.h"   /* + strlcpy, strlcat prototypes */
32
33
#ifndef HAVE_STRLCPY    /* + */
34
/*
35
 * Copy src to string dst of size siz.  At most siz-1 characters
36
 * will be copied.  Always NUL terminates (unless siz == 0).
37
 * Returns strlen(src); if retval >= siz, truncation occurred.
38
 * 
39
 * Quoting from FreeBSD 14.2 `man strlcpy`:
40
 * If the src and dst strings overlap, the behavior is undefined.
41
 */
42
size_t
43
strlcpy(char *dst, const char *src, size_t siz)
44
2
{
45
2
  char *d = dst;
46
2
  const char *s = src;
47
2
  size_t n = siz;
48
49
  /* Copy as many bytes as will fit */
50
2
  if (n != 0) {
51
8
    while (--n != 0) {
52
8
      if ((*d++ = *s++) == '\0')
53
2
        break;
54
8
    }
55
2
  }
56
57
  /* Not enough room in dst, add NUL and traverse rest of src */
58
2
  if (n == 0) {
59
0
    if (siz != 0)
60
0
      *d = '\0';   /* NUL-terminate dst */
61
0
    while (*s++)
62
0
      ;
63
0
  }
64
65
2
  return(s - src - 1);  /* count does not include NUL */
66
2
}
67
#endif        /* + */
68
69
70
/*  $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $  */
71
72
/*
73
 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
74
 *
75
 * Permission to use, copy, modify, and distribute this software for any
76
 * purpose with or without fee is hereby granted, provided that the above
77
 * copyright notice and this permission notice appear in all copies.
78
 *
79
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
80
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
81
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
82
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
83
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
84
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
85
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
86
 */
87
88
/* #include <sys/types.h> */  /* + */
89
/* #include <string.h> */ /* + */
90
91
#ifndef HAVE_STRLCAT    /* + */
92
/*
93
 * Appends src to string dst of size siz (unlike strncat, siz is the
94
 * full size of dst, not space left).  At most siz-1 characters
95
 * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
96
 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
97
 * If retval >= siz, truncation occurred.
98
 *
99
 * Quoting from FreeBSD 14.2 `man strlcat`:
100
 * If the src and dst strings overlap, the behavior is undefined.
101
 */
102
size_t
103
strlcat(char *dst, const char *src, size_t siz)
104
0
{
105
0
  char *d = dst;
106
0
  const char *s = src;
107
0
  size_t n = siz;
108
0
  size_t dlen;
109
110
  /* Find the end of dst and adjust bytes left but don't go past end */
111
0
  while (n-- != 0 && *d != '\0')
112
0
    d++;
113
0
  dlen = d - dst;
114
0
  n = siz - dlen;
115
116
0
  if (n == 0)
117
0
    return(dlen + strlen(s));
118
0
  while (*s != '\0') {
119
0
    if (n != 1) {
120
0
      *d++ = *s;
121
0
      n--;
122
0
    }
123
0
    s++;
124
0
  }
125
0
  *d = '\0';
126
127
0
  return(dlen + (s - src)); /* count does not include NUL */
128
0
}
129
#endif        /* + */