Coverage Report

Created: 2025-07-18 07:03

/src/bind9/lib/isc/string.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
 * SPDX-License-Identifier: MPL-2.0
5
 *
6
 * This Source Code Form is subject to the terms of the Mozilla Public
7
 * License, v. 2.0. If a copy of the MPL was not distributed with this
8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9
 *
10
 * See the COPYRIGHT file distributed with this work for additional
11
 * information regarding copyright ownership.
12
 */
13
14
/*
15
 * SPDX-License-Identifier: BSD-3-Clause
16
 *
17
 * Copyright (c) 2001 Mike Barcroft <mike@FreeBSD.org>
18
 * Copyright (c) 1990, 1993
19
 *  The Regents of the University of California.  All rights reserved.
20
 *
21
 * This code is derived from software contributed to Berkeley by
22
 * Chris Torek.
23
 *
24
 * Redistribution and use in source and binary forms, with or without
25
 * modification, are permitted provided that the following conditions
26
 * are met:
27
 * 1. Redistributions of source code must retain the above copyright
28
 *    notice, this list of conditions and the following disclaimer.
29
 * 2. Redistributions in binary form must reproduce the above copyright
30
 *    notice, this list of conditions and the following disclaimer in the
31
 *    documentation and/or other materials provided with the distribution.
32
 * 3. Neither the name of the University nor the names of its contributors
33
 *    may be used to endorse or promote products derived from this software
34
 *    without specific prior written permission.
35
 *
36
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46
 * SUCH DAMAGE.
47
 */
48
49
/*! \file */
50
51
#ifdef _GNU_SOURCE
52
#undef _GNU_SOURCE
53
#endif /* ifdef _GNU_SOURCE */
54
#include <string.h>
55
56
#include <isc/string.h> /* IWYU pragma: keep */
57
58
/*
59
 * We undef _GNU_SOURCE above to get the POSIX strerror_r()
60
 */
61
int
62
0
isc_string_strerror_r(int errnum, char *buf, size_t buflen) {
63
0
  return strerror_r(errnum, buf, buflen);
64
0
}
65
66
#if !defined(HAVE_STRLCPY)
67
size_t
68
3.36M
strlcpy(char *dst, const char *src, size_t size) {
69
3.36M
  char *d = dst;
70
3.36M
  const char *s = src;
71
3.36M
  size_t n = size;
72
73
  /* Copy as many bytes as will fit */
74
3.36M
  if (n != 0U && --n != 0U) {
75
68.1M
    do {
76
68.1M
      if ((*d++ = *s++) == 0) {
77
156k
        break;
78
156k
      }
79
68.1M
    } while (--n != 0U);
80
3.36M
  }
81
82
  /* Not enough room in dst, add NUL and traverse rest of src */
83
3.36M
  if (n == 0U) {
84
3.21M
    if (size != 0U) {
85
3.21M
      *d = '\0'; /* NUL-terminate dst */
86
3.21M
    }
87
3.21M
    while (*s++) {
88
493
    }
89
3.21M
  }
90
91
3.36M
  return s - src - 1; /* count does not include NUL */
92
3.36M
}
93
#endif /* !defined(HAVE_STRLCPY) */
94
95
#if !defined(HAVE_STRLCAT)
96
size_t
97
2
strlcat(char *dst, const char *src, size_t size) {
98
2
  char *d = dst;
99
2
  const char *s = src;
100
2
  size_t n = size;
101
2
  size_t dlen;
102
103
  /* Find the end of dst and adjust bytes left but don't go past end */
104
88
  while (n-- != 0U && *d != '\0') {
105
86
    d++;
106
86
  }
107
2
  dlen = d - dst;
108
2
  n = size - dlen;
109
110
2
  if (n == 0U) {
111
0
    return dlen + strlen(s);
112
0
  }
113
10
  while (*s != '\0') {
114
8
    if (n != 1U) {
115
8
      *d++ = *s;
116
8
      n--;
117
8
    }
118
8
    s++;
119
8
  }
120
2
  *d = '\0';
121
122
2
  return dlen + (s - src); /* count does not include NUL */
123
2
}
124
#endif /* !defined(HAVE_STRLCAT) */
125
126
#if !defined(HAVE_STRNSTR)
127
char *
128
0
strnstr(const char *s, const char *find, size_t slen) {
129
0
  char c, sc;
130
0
  size_t len;
131
132
0
  if ((c = *find++) != '\0') {
133
0
    len = strlen(find);
134
0
    do {
135
0
      do {
136
0
        if (slen-- < 1 || (sc = *s++) == '\0') {
137
0
          return NULL;
138
0
        }
139
0
      } while (sc != c);
140
0
      if (len > slen) {
141
0
        return NULL;
142
0
      }
143
0
    } while (strncmp(s, find, len) != 0);
144
0
    s--;
145
0
  }
146
0
  return (char *)s;
147
0
}
148
#endif