Coverage Report

Created: 2026-12-01 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/plugins/sudoers/strlist.c
Line
Count
Source
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2018 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 <stdlib.h>
22
#include <string.h>
23
24
#include <sudo_compat.h>
25
#include <sudo_queue.h>
26
#include <sudo_util.h>
27
#include <sudoers_debug.h>
28
#include <strlist.h>
29
30
struct sudoers_string *
31
sudoers_string_alloc(const char *s)
32
279k
{
33
279k
    struct sudoers_string *cs;
34
279k
    debug_decl(sudoers_string_alloc, SUDOERS_DEBUG_UTIL);
35
36
279k
    if ((cs = malloc(sizeof(*cs))) != NULL) {
37
279k
  if ((cs->str = strdup(s)) == NULL) {
38
0
      free(cs);
39
0
      cs = NULL;
40
0
  }
41
279k
    }
42
43
279k
    debug_return_ptr(cs);
44
279k
}
45
46
void
47
sudoers_string_free(struct sudoers_string *cs)
48
279k
{
49
279k
    if (cs != NULL) {
50
279k
  free(cs->str);
51
279k
  free(cs);
52
279k
    }
53
279k
}
54
55
struct sudoers_str_list *
56
str_list_alloc(void)
57
202k
{
58
202k
    struct sudoers_str_list *strlist;
59
202k
    debug_decl(str_list_alloc, SUDOERS_DEBUG_UTIL);
60
61
202k
    strlist = malloc(sizeof(*strlist));
62
202k
    if (strlist != NULL) {
63
202k
  STAILQ_INIT(strlist);
64
202k
  strlist->refcnt = 1;
65
202k
    }
66
67
202k
    debug_return_ptr(strlist);
68
202k
}
69
70
void
71
str_list_free(void *v)
72
319k
{
73
319k
    struct sudoers_str_list *strlist = v;
74
319k
    struct sudoers_string *first;
75
319k
    debug_decl(str_list_free, SUDOERS_DEBUG_UTIL);
76
77
319k
    if (strlist != NULL) {
78
319k
  if (--strlist->refcnt == 0) {
79
481k
      while ((first = STAILQ_FIRST(strlist)) != NULL) {
80
279k
    STAILQ_REMOVE_HEAD(strlist, entries);
81
279k
    sudoers_string_free(first);
82
279k
      }
83
202k
      free(strlist);
84
202k
  }
85
319k
    }
86
319k
    debug_return;
87
319k
}