Coverage Report

Created: 2023-05-19 06:16

/src/ntp-dev/libntp/strl_obsd.c
Line
Count
Source (jump to first uncovered line)
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
size_t
40
strlcpy(char *dst, const char *src, size_t siz)
41
43
{
42
43
  char *d = dst;
43
43
  const char *s = src;
44
43
  size_t n = siz;
45
46
  /* Copy as many bytes as will fit */
47
43
  if (n != 0) {
48
583
    while (--n != 0) {
49
580
      if ((*d++ = *s++) == '\0')
50
40
        break;
51
580
    }
52
43
  }
53
54
  /* Not enough room in dst, add NUL and traverse rest of src */
55
43
  if (n == 0) {
56
3
    if (siz != 0)
57
3
      *d = '\0';   /* NUL-terminate dst */
58
3
    while (*s++)
59
0
      ;
60
3
  }
61
62
43
  return(s - src - 1);  /* count does not include NUL */
63
43
}
64
#endif        /* + */
65
66
67
/*  $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $  */
68
69
/*
70
 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
71
 *
72
 * Permission to use, copy, modify, and distribute this software for any
73
 * purpose with or without fee is hereby granted, provided that the above
74
 * copyright notice and this permission notice appear in all copies.
75
 *
76
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
77
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
78
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
79
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
80
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
81
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
82
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
83
 */
84
85
/* #include <sys/types.h> */  /* + */
86
/* #include <string.h> */ /* + */
87
88
#ifndef HAVE_STRLCAT    /* + */
89
/*
90
 * Appends src to string dst of size siz (unlike strncat, siz is the
91
 * full size of dst, not space left).  At most siz-1 characters
92
 * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
93
 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
94
 * If retval >= siz, truncation occurred.
95
 */
96
size_t
97
strlcat(char *dst, const char *src, size_t siz)
98
0
{
99
0
  char *d = dst;
100
0
  const char *s = src;
101
0
  size_t n = siz;
102
0
  size_t dlen;
103
104
  /* Find the end of dst and adjust bytes left but don't go past end */
105
0
  while (n-- != 0 && *d != '\0')
106
0
    d++;
107
0
  dlen = d - dst;
108
0
  n = siz - dlen;
109
110
0
  if (n == 0)
111
0
    return(dlen + strlen(s));
112
0
  while (*s != '\0') {
113
0
    if (n != 1) {
114
0
      *d++ = *s;
115
0
      n--;
116
0
    }
117
0
    s++;
118
0
  }
119
0
  *d = '\0';
120
121
0
  return(dlen + (s - src)); /* count does not include NUL */
122
0
}
123
#endif        /* + */