Coverage Report

Created: 2025-10-10 07:07

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