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