/src/sudo/lib/util/rcstr.c
Line | Count | Source (jump to first uncovered line) |
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 | | /* |
20 | | * This is an open source non-commercial project. Dear PVS-Studio, please check it. |
21 | | * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
22 | | */ |
23 | | |
24 | | #include <config.h> |
25 | | |
26 | | #include <stddef.h> |
27 | | #include <stdlib.h> |
28 | | #include <string.h> |
29 | | |
30 | | #include <sudo_compat.h> |
31 | | #include <sudo_debug.h> |
32 | | #include <sudo_util.h> |
33 | | |
34 | | /* Trivial reference-counted strings. */ |
35 | | struct rcstr { |
36 | | int refcnt; |
37 | | char str[]; |
38 | | }; |
39 | | |
40 | | /* |
41 | | * Allocate a reference-counted string and copy src to it. |
42 | | * Returns the newly-created string with a refcnt of 1. |
43 | | */ |
44 | | char * |
45 | | sudo_rcstr_dup(const char *src) |
46 | 0 | { |
47 | 0 | size_t len = strlen(src); |
48 | 0 | char *dst; |
49 | 0 | debug_decl(sudo_rcstr_dup, SUDO_DEBUG_UTIL); |
50 | |
|
51 | 0 | dst = sudo_rcstr_alloc(len); |
52 | 0 | if (dst != NULL) { |
53 | 0 | memcpy(dst, src, len); |
54 | 0 | dst[len] = '\0'; |
55 | 0 | } |
56 | 0 | debug_return_ptr(dst); |
57 | 0 | } |
58 | | |
59 | | char * |
60 | | sudo_rcstr_alloc(size_t len) |
61 | 0 | { |
62 | 0 | struct rcstr *rcs; |
63 | 0 | debug_decl(sudo_rcstr_dup, SUDO_DEBUG_UTIL); |
64 | |
|
65 | 0 | rcs = malloc(sizeof(struct rcstr) + len + 1); |
66 | 0 | if (rcs == NULL) |
67 | 0 | return NULL; |
68 | | |
69 | 0 | rcs->refcnt = 1; |
70 | 0 | rcs->str[0] = '\0'; |
71 | | /* cppcheck-suppress memleak */ |
72 | 0 | debug_return_ptr(rcs->str); // -V773 |
73 | 0 | } |
74 | | |
75 | | char * |
76 | | sudo_rcstr_addref(const char *s) |
77 | 19.0k | { |
78 | 19.0k | struct rcstr *rcs; |
79 | 19.0k | debug_decl(sudo_rcstr_dup, SUDO_DEBUG_UTIL); |
80 | | |
81 | 19.0k | if (s == NULL) |
82 | 19.0k | debug_return_ptr(NULL); |
83 | | |
84 | 0 | rcs = __containerof((const void *)s, struct rcstr, str); |
85 | 0 | rcs->refcnt++; |
86 | 0 | debug_return_ptr(rcs->str); |
87 | 0 | } |
88 | | |
89 | | void |
90 | | sudo_rcstr_delref(const char *s) |
91 | 100k | { |
92 | 100k | struct rcstr *rcs; |
93 | 100k | debug_decl(sudo_rcstr_dup, SUDO_DEBUG_UTIL); |
94 | | |
95 | 100k | if (s != NULL) { |
96 | 0 | rcs = __containerof((const void *)s, struct rcstr, str); |
97 | 0 | if (--rcs->refcnt == 0) { |
98 | 0 | rcs->str[0] = '\0'; |
99 | 0 | free(rcs); |
100 | 0 | } |
101 | 0 | } |
102 | 100k | debug_return; |
103 | 100k | } |