Coverage Report

Created: 2023-06-07 06:46

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