/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  | 2.97k  | { | 
42  | 2.97k  |     size_t len = strlen(src);  | 
43  | 2.97k  |     char *dst;  | 
44  | 2.97k  |     debug_decl(sudo_rcstr_dup, SUDO_DEBUG_UTIL);  | 
45  |  |  | 
46  | 2.97k  |     dst = sudo_rcstr_alloc(len);  | 
47  | 2.97k  |     if (dst != NULL) { | 
48  | 2.97k  |         memcpy(dst, src, len);  | 
49  | 2.97k  |         dst[len] = '\0';  | 
50  | 2.97k  |     }  | 
51  | 2.97k  |     debug_return_ptr(dst);  | 
52  | 2.97k  | }  | 
53  |  |  | 
54  |  | char *  | 
55  |  | sudo_rcstr_alloc(size_t len)  | 
56  | 6.49k  | { | 
57  | 6.49k  |     struct rcstr *rcs;  | 
58  | 6.49k  |     debug_decl(sudo_rcstr_dup, SUDO_DEBUG_UTIL);  | 
59  |  |  | 
60  | 6.49k  |     rcs = malloc(sizeof(struct rcstr) + len + 1);  | 
61  | 6.49k  |     if (rcs == NULL)  | 
62  | 0  |   return NULL;  | 
63  |  |  | 
64  | 6.49k  |     rcs->refcnt = 1;  | 
65  | 6.49k  |     rcs->str[0] = '\0';  | 
66  |  |     /* cppcheck-suppress memleak */  | 
67  | 6.49k  |     debug_return_ptr(rcs->str); // -V773  | 
68  | 6.49k  | }  | 
69  |  |  | 
70  |  | char *  | 
71  |  | sudo_rcstr_addref(const char *s)  | 
72  | 154k  | { | 
73  | 154k  |     struct rcstr *rcs;  | 
74  | 154k  |     debug_decl(sudo_rcstr_dup, SUDO_DEBUG_UTIL);  | 
75  |  |  | 
76  | 154k  |     if (s == NULL)  | 
77  | 18.7k  |   debug_return_ptr(NULL);  | 
78  |  |  | 
79  | 136k  |     rcs = __containerof((const void *)s, struct rcstr, str);  | 
80  | 136k  |     rcs->refcnt++;  | 
81  | 136k  |     debug_return_ptr(rcs->str);  | 
82  | 136k  | }  | 
83  |  |  | 
84  |  | void  | 
85  |  | sudo_rcstr_delref(const char *s)  | 
86  | 253k  | { | 
87  | 253k  |     struct rcstr *rcs;  | 
88  | 253k  |     debug_decl(sudo_rcstr_dup, SUDO_DEBUG_UTIL);  | 
89  |  |  | 
90  | 253k  |     if (s != NULL) { | 
91  | 142k  |   rcs = __containerof((const void *)s, struct rcstr, str);  | 
92  | 142k  |   if (--rcs->refcnt == 0) { | 
93  | 6.49k  |       rcs->str[0] = '\0';  | 
94  | 6.49k  |       free(rcs);  | 
95  | 6.49k  |   }  | 
96  | 142k  |     }  | 
97  | 253k  |     debug_return;  | 
98  | 253k  | }  |