Coverage Report

Created: 2025-10-10 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/plugins/sudoers/strvec_join.c
Line
Count
Source
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2021 Todd C. Miller <Todd.Miller@sudo.ws>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <config.h>
20
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
25
#include <sudoers.h>
26
27
#ifdef HAVE_STRLCPY
28
# define cpy_default  strlcpy
29
#else
30
1.02k
# define cpy_default  sudo_strlcpy
31
#endif
32
33
/*
34
 * Join a NULL-terminated array of strings using the specified separator
35
 * char.  If non-NULL, the copy function must have strlcpy-like semantics.
36
 */
37
char *
38
strvec_join(char *const argv[], char sep, size_t (*cpy)(char * restrict, const char * restrict, size_t))
39
1.78k
{
40
1.78k
    char *dst, *result = NULL;
41
1.78k
    char *const *av;
42
1.78k
    size_t n, size = 0;
43
1.78k
    debug_decl(strvec_join, SUDOERS_DEBUG_UTIL);
44
45
12.5M
    for (av = argv; *av != NULL; av++)
46
12.5M
  size += strlen(*av) + 1;
47
1.78k
    if (size == 0 || (result = malloc(size)) == NULL) {
48
0
  sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
49
0
  debug_return_ptr(NULL);
50
0
    }
51
52
1.78k
    if (cpy == NULL)
53
1.02k
  cpy = cpy_default;
54
12.5M
    for (dst = result, av = argv; *av != NULL; av++) {
55
12.5M
  n = cpy(dst, *av, size);
56
12.5M
  if (n >= size) {
57
0
      sudo_warnx(U_("internal error, %s overflow"), __func__);
58
0
      free(result);
59
0
      debug_return_ptr(NULL);
60
0
  }
61
12.5M
  dst += n;
62
12.5M
  size -= n;
63
12.5M
  *dst++ = sep;
64
12.5M
  size--;
65
12.5M
    }
66
1.78k
    dst[-1] = '\0';
67
68
1.78k
    debug_return_str(result);
69
1.78k
}