• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 23966986054

03 Apr 2026 09:22PM UTC coverage: 72.107% (-0.3%) from 72.362%
23966986054

push

github

daandemeyer
fd-util: Add missing assert()

1 of 1 new or added line in 1 file covered. (100.0%)

7891 existing lines in 120 files now uncovered.

318256 of 441368 relevant lines covered (72.11%)

1186882.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.72
/src/basic/alloc-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <malloc.h>
4

5
#include "alloc-util.h"
6

7
void* memdup(const void *p, size_t l) {
614,737✔
8
        void *ret;
614,737✔
9

10
        assert(l == 0 || p);
614,737✔
11

12
        ret = malloc(l ?: 1);
614,737✔
13
        if (!ret)
614,737✔
14
                return NULL;
15

16
        return memcpy_safe(ret, p, l);
614,737✔
17
}
18

19
void* memdup_suffix0(const void *p, size_t l) {
3,613,726✔
20
        void *ret;
3,613,726✔
21

22
        assert(l == 0 || p);
3,613,726✔
23

24
        /* The same as memdup() but place a safety NUL byte after the allocated memory */
25

26
        if (_unlikely_(l == SIZE_MAX)) /* prevent overflow */
3,613,726✔
27
                return NULL;
28

29
        ret = malloc(l + 1);
3,613,726✔
30
        if (!ret)
3,613,726✔
31
                return NULL;
32

33
        ((uint8_t*) ret)[l] = 0;
3,613,726✔
34
        return memcpy_safe(ret, p, l);
3,613,726✔
35
}
36

37
size_t malloc_sizeof_safe(void **xp) {
642,607,410✔
38
        POINTER_MAY_BE_NULL(xp);
642,607,410✔
39

40
        if (_unlikely_(!xp || !*xp))
642,607,410✔
41
                return 0;
42

43
        size_t sz = malloc_usable_size(*xp);
642,189,355✔
44
        *xp = expand_to_usable(*xp, sz);
642,189,355✔
45
        /* GCC doesn't see the _returns_nonnull_ when built with ubsan, so yet another hint to make it doubly
46
         * clear that expand_to_usable won't return NULL.
47
         * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79265 */
48
        if (!*xp)
642,189,355✔
UNCOV
49
                assert_not_reached();
×
50
        return sz;
51
}
52

53
void* expand_to_usable(void *ptr, size_t newsize _unused_) {
643,311,151✔
54
        return ptr;
643,311,151✔
55
}
56

57
void* realloc0(void *p, size_t new_size) {
69,817✔
58
        size_t old_size;
69,817✔
59
        void *q;
69,817✔
60

61
        /* Like realloc(), but initializes anything appended to zero */
62

63
        old_size = MALLOC_SIZEOF_SAFE(p);
69,817✔
64

65
        q = realloc(p, new_size);
69,817✔
66
        if (!q)
69,817✔
67
                return NULL;
69,817✔
68

69
        new_size = MALLOC_SIZEOF_SAFE(q); /* Update with actually allocated space */
69,817✔
70

71
        if (new_size > old_size)
69,817✔
72
                memset((uint8_t*) q + old_size, 0, new_size - old_size);
63,883✔
73

74
        return q;
69,817✔
75
}
76

77
void* greedy_realloc(
651,675,385✔
78
                void **p,
79
                size_t need,
80
                size_t size) {
81

82
        size_t newalloc;
651,675,385✔
83
        void *q;
651,675,385✔
84

85
        assert(p);
651,675,385✔
86

87
        /* We use malloc_usable_size() for determining the current allocated size. On all systems we care
88
         * about this should be safe to rely on. Should there ever arise the need to avoid relying on this we
89
         * can instead locally fall back to realloc() on every call, rounded up to the next exponent of 2 or
90
         * so. */
91

92
        if (*p && (size == 0 || (MALLOC_SIZEOF_SAFE(*p) / size >= need)))
651,675,385✔
93
                return *p;
616,813,188✔
94

95
        if (_unlikely_(need > SIZE_MAX/2)) /* Overflow check */
34,862,197✔
96
                return NULL;
97
        newalloc = need * 2;
34,862,197✔
98

99
        if (!MUL_ASSIGN_SAFE(&newalloc, size))
34,862,197✔
100
                return NULL;
101

102
        if (newalloc < 64) /* Allocate at least 64 bytes */
34,862,197✔
103
                newalloc = 64;
29,431,373✔
104

105
        q = realloc(*p, newalloc);
34,862,197✔
106
        if (!q)
34,862,197✔
107
                return NULL;
108

109
        return *p = q;
34,862,197✔
110
}
111

112
void* greedy_realloc0(
4,325,303✔
113
                void **p,
114
                size_t need,
115
                size_t size) {
116

117
        size_t before, after;
4,325,303✔
118
        uint8_t *q;
4,325,303✔
119

120
        assert(p);
4,325,303✔
121

122
        before = MALLOC_SIZEOF_SAFE(*p); /* malloc_usable_size() will return 0 on NULL input, as per docs */
4,325,303✔
123

124
        q = greedy_realloc(p, need, size);
4,325,303✔
125
        if (!q)
4,325,303✔
126
                return NULL;
4,325,303✔
127

128
        after = MALLOC_SIZEOF_SAFE(q);
4,325,303✔
129

130
        if (size == 0) /* avoid division by zero */
4,325,303✔
131
                before = 0;
132
        else
133
                before = (before / size) * size; /* Round down */
4,325,303✔
134

135
        if (after > before)
4,325,303✔
136
                memzero(q + before, after - before);
2,357,979✔
137

138
        return q;
4,325,303✔
139
}
140

141
void* greedy_realloc_append(
14,655✔
142
                void **p,
143
                size_t *n_p,
144
                const void *from,
145
                size_t n_from,
146
                size_t size) {
147

148
        uint8_t *q;
14,655✔
149

150
        assert(p);
14,655✔
151
        assert(n_p);
14,655✔
152
        assert(from || n_from == 0);
14,655✔
153

154
        if (n_from > SIZE_MAX - *n_p)
14,655✔
155
                return NULL;
156

157
        q = greedy_realloc(p, *n_p + n_from, size);
14,655✔
158
        if (!q)
14,655✔
159
                return NULL;
160

161
        memcpy_safe(q + *n_p * size, from, n_from * size);
14,655✔
162

163
        *n_p += n_from;
14,655✔
164

165
        return q;
14,655✔
166
}
167

168
void free_many(void **p, size_t n) {
98,094✔
169
        assert(p || n == 0);
98,094✔
170

171
        FOREACH_ARRAY(i, p, n)
1,556,609✔
172
                *i = mfree(*i);
1,458,515✔
173
}
98,094✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc