/src/sudo/lib/util/rcstr.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2016-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 <stddef.h> |
22 | | #include <stdlib.h> |
23 | | #include <string.h> |
24 | | |
25 | | #include <sudo_compat.h> |
26 | | #include <sudo_debug.h> |
27 | | #include <sudo_util.h> |
28 | | |
29 | | /* Trivial reference-counted strings. */ |
30 | | struct rcstr { |
31 | | int refcnt; |
32 | | char str[]; |
33 | | }; |
34 | | |
35 | | /* |
36 | | * Allocate a reference-counted string and copy src to it. |
37 | | * Returns the newly-created string with a refcnt of 1. |
38 | | */ |
39 | | char * |
40 | | sudo_rcstr_dup(const char *src) |
41 | 8 | { |
42 | 8 | size_t len = strlen(src); |
43 | 8 | char *dst; |
44 | 8 | debug_decl(sudo_rcstr_dup, SUDO_DEBUG_UTIL); |
45 | | |
46 | 8 | dst = sudo_rcstr_alloc(len); |
47 | 8 | if (dst != NULL) { |
48 | 8 | memcpy(dst, src, len); |
49 | 8 | dst[len] = '\0'; |
50 | 8 | } |
51 | 8 | debug_return_ptr(dst); |
52 | 8 | } |
53 | | |
54 | | char * |
55 | | sudo_rcstr_alloc(size_t len) |
56 | 8 | { |
57 | 8 | struct rcstr *rcs; |
58 | 8 | debug_decl(sudo_rcstr_dup, SUDO_DEBUG_UTIL); |
59 | | |
60 | 8 | rcs = malloc(sizeof(struct rcstr) + len + 1); |
61 | 8 | if (rcs == NULL) |
62 | 0 | return NULL; |
63 | | |
64 | 8 | rcs->refcnt = 1; |
65 | 8 | rcs->str[0] = '\0'; |
66 | | /* cppcheck-suppress memleak */ |
67 | 8 | debug_return_ptr(rcs->str); // -V773 |
68 | 8 | } |
69 | | |
70 | | char * |
71 | | sudo_rcstr_addref(const char *s) |
72 | 0 | { |
73 | 0 | struct rcstr *rcs; |
74 | 0 | debug_decl(sudo_rcstr_dup, SUDO_DEBUG_UTIL); |
75 | |
|
76 | 0 | if (s == NULL) |
77 | 0 | debug_return_ptr(NULL); |
78 | | |
79 | 0 | rcs = __containerof((const void *)s, struct rcstr, str); |
80 | 0 | rcs->refcnt++; |
81 | 0 | debug_return_ptr(rcs->str); |
82 | 0 | } |
83 | | |
84 | | void |
85 | | sudo_rcstr_delref(const char *s) |
86 | 32 | { |
87 | 32 | struct rcstr *rcs; |
88 | 32 | debug_decl(sudo_rcstr_dup, SUDO_DEBUG_UTIL); |
89 | | |
90 | 32 | if (s != NULL) { |
91 | 8 | rcs = __containerof((const void *)s, struct rcstr, str); |
92 | 8 | if (--rcs->refcnt == 0) { |
93 | 8 | rcs->str[0] = '\0'; |
94 | 8 | free(rcs); |
95 | 8 | } |
96 | 8 | } |
97 | 32 | debug_return; |
98 | 32 | } |