Coverage Report

Created: 2025-07-11 06:58

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