Coverage Report

Created: 2025-10-13 06:08

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
371k
{
33
371k
    struct sudoers_string *cs;
34
371k
    debug_decl(sudoers_string_alloc, SUDOERS_DEBUG_UTIL);
35
36
371k
    if ((cs = malloc(sizeof(*cs))) != NULL) {
37
371k
  if ((cs->str = strdup(s)) == NULL) {
38
0
      free(cs);
39
0
      cs = NULL;
40
0
  }
41
371k
    }
42
43
371k
    debug_return_ptr(cs);
44
371k
}
45
46
void
47
sudoers_string_free(struct sudoers_string *cs)
48
371k
{
49
371k
    if (cs != NULL) {
50
371k
  free(cs->str);
51
371k
  free(cs);
52
371k
    }
53
371k
}
54
55
struct sudoers_str_list *
56
str_list_alloc(void)
57
389k
{
58
389k
    struct sudoers_str_list *strlist;
59
389k
    debug_decl(str_list_alloc, SUDOERS_DEBUG_UTIL);
60
61
389k
    strlist = malloc(sizeof(*strlist));
62
389k
    if (strlist != NULL) {
63
389k
  STAILQ_INIT(strlist);
64
389k
  strlist->refcnt = 1;
65
389k
    }
66
67
389k
    debug_return_ptr(strlist);
68
389k
}
69
70
void
71
str_list_free(void *v)
72
629k
{
73
629k
    struct sudoers_str_list *strlist = v;
74
629k
    struct sudoers_string *first;
75
629k
    debug_decl(str_list_free, SUDOERS_DEBUG_UTIL);
76
77
629k
    if (strlist != NULL) {
78
629k
  if (--strlist->refcnt == 0) {
79
761k
      while ((first = STAILQ_FIRST(strlist)) != NULL) {
80
371k
    STAILQ_REMOVE_HEAD(strlist, entries);
81
371k
    sudoers_string_free(first);
82
371k
      }
83
389k
      free(strlist);
84
389k
  }
85
629k
    }
86
629k
    debug_return;
87
629k
}