Coverage Report

Created: 2023-06-07 06:47

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