Coverage Report

Created: 2025-07-18 06:08

/src/avahi/avahi-common/malloc.c
Line
Count
Source (jump to first uncovered line)
1
/***
2
  This file is part of avahi.
3
4
  avahi is free software; you can redistribute it and/or modify it
5
  under the terms of the GNU Lesser General Public License as
6
  published by the Free Software Foundation; either version 2.1 of the
7
  License, or (at your option) any later version.
8
9
  avahi is distributed in the hope that it will be useful, but WITHOUT
10
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11
  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12
  Public License for more details.
13
14
  You should have received a copy of the GNU Lesser General Public
15
  License along with avahi; if not, write to the Free Software
16
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17
  USA.
18
***/
19
20
#ifdef HAVE_CONFIG_H
21
#include <config.h>
22
#endif
23
24
#include <stdlib.h>
25
#include <string.h>
26
#include <assert.h>
27
#include <stdio.h>
28
#include <unistd.h>
29
30
#include "malloc.h"
31
32
#ifndef va_copy
33
#ifdef __va_copy
34
#define va_copy(DEST,SRC) __va_copy((DEST),(SRC))
35
#else
36
#define va_copy(DEST,SRC) memcpy(&(DEST), &(SRC), sizeof(va_list))
37
#endif
38
#endif
39
40
static const AvahiAllocator *allocator = NULL;
41
42
static void oom(void) AVAHI_GCC_NORETURN;
43
44
0
static void oom(void) {
45
46
0
    static const char msg[] = "Out of memory, aborting ...\n";
47
0
    const char *n = msg;
48
49
0
    while (strlen(n) > 0) {
50
0
        ssize_t r;
51
52
0
        if ((r = write(2, n, strlen(n))) < 0)
53
0
            break;
54
55
0
        n += r;
56
0
    }
57
58
0
    abort();
59
0
}
60
61
/* Default implementation for avahi_malloc() */
62
6.31M
static void* xmalloc(size_t size) {
63
6.31M
    void *p;
64
65
6.31M
    if (size == 0)
66
0
        return NULL;
67
68
6.31M
    if (!(p = malloc(size)))
69
0
        oom();
70
71
6.31M
    return p;
72
6.31M
}
73
74
/* Default implementation for avahi_realloc() */
75
15.6k
static void *xrealloc(void *p, size_t size) {
76
77
15.6k
    if (size == 0) {
78
0
        free(p);
79
0
        return NULL;
80
0
    }
81
82
15.6k
    if (!(p = realloc(p, size)))
83
0
        oom();
84
85
15.6k
    return p;
86
15.6k
}
87
88
/* Default implementation for avahi_calloc() */
89
2.39k
static void *xcalloc(size_t nmemb, size_t size) {
90
2.39k
    void *p;
91
92
2.39k
    if (size == 0 || nmemb == 0)
93
0
        return NULL;
94
95
2.39k
    if (!(p = calloc(nmemb, size)))
96
0
        oom();
97
98
2.39k
    return p;
99
2.39k
}
100
101
6.31M
void *avahi_malloc(size_t size) {
102
103
6.31M
    if (size <= 0)
104
0
        return NULL;
105
106
6.31M
    if (!allocator)
107
6.31M
        return xmalloc(size);
108
109
0
    assert(allocator->malloc);
110
0
    return allocator->malloc(size);
111
0
}
112
113
2.39k
void *avahi_malloc0(size_t size) {
114
2.39k
    void *p;
115
116
2.39k
    if (size <= 0)
117
0
        return NULL;
118
119
2.39k
    if (!allocator)
120
2.39k
        return xcalloc(1, size);
121
122
0
    if (allocator->calloc)
123
0
        return allocator->calloc(1, size);
124
125
0
    assert(allocator->malloc);
126
0
    if ((p = allocator->malloc(size)))
127
0
        memset(p, 0, size);
128
129
0
    return p;
130
0
}
131
132
6.36M
void avahi_free(void *p) {
133
134
6.36M
    if (!p)
135
48.5k
        return;
136
137
6.31M
    if (!allocator) {
138
6.31M
        free(p);
139
6.31M
        return;
140
6.31M
    }
141
142
0
    assert(allocator->free);
143
0
    allocator->free(p);
144
0
}
145
146
15.6k
void *avahi_realloc(void *p, size_t size) {
147
148
15.6k
    if (size == 0) {
149
0
        avahi_free(p);
150
0
        return NULL;
151
0
    }
152
153
15.6k
    if (!allocator)
154
15.6k
        return xrealloc(p, size);
155
156
0
    assert(allocator->realloc);
157
0
    return allocator->realloc(p, size);
158
0
}
159
160
262k
char *avahi_strdup(const char *s) {
161
262k
    char *r;
162
262k
    size_t size;
163
164
262k
    if (!s)
165
0
        return NULL;
166
167
262k
    size = strlen(s);
168
262k
    if (!(r = avahi_malloc(size+1)))
169
0
        return NULL;
170
171
262k
    memcpy(r, s, size+1);
172
262k
    return r;
173
262k
}
174
175
5.98k
char *avahi_strndup(const char *s, size_t max) {
176
5.98k
    char *r;
177
5.98k
    size_t size;
178
5.98k
    const char *p;
179
180
5.98k
    if (!s)
181
0
        return NULL;
182
183
5.98k
    for (p = s, size = 0;
184
20.5k
         size < max && *p;
185
14.6k
         p++, size++);
186
187
5.98k
    if (!(r = avahi_new(char, size+1)))
188
0
        return NULL;
189
190
5.98k
    memcpy(r, s, size);
191
5.98k
    r[size] = 0;
192
5.98k
    return r;
193
5.98k
}
194
195
/* Change the allocator */
196
0
void avahi_set_allocator(const AvahiAllocator *a) {
197
0
    allocator = a;
198
0
}
199
200
207k
char *avahi_strdup_vprintf(const char *fmt, va_list ap) {
201
207k
    size_t len = 80;
202
207k
    char *buf;
203
204
207k
    assert(fmt);
205
206
207k
    if (!(buf = avahi_malloc(len)))
207
0
        return NULL;
208
209
223k
    for (;;) {
210
223k
        int n;
211
223k
        char *nbuf;
212
223k
        va_list ap2;
213
214
223k
        va_copy (ap2, ap);
215
223k
        n = vsnprintf(buf, len, fmt, ap2);
216
223k
        va_end (ap2);
217
218
223k
        if (n >= 0 && n < (int) len)
219
207k
            return buf;
220
221
15.6k
        if (n >= 0)
222
15.6k
            len = n+1;
223
0
        else
224
0
            len *= 2;
225
226
15.6k
        if (!(nbuf = avahi_realloc(buf, len))) {
227
0
            avahi_free(buf);
228
0
            return NULL;
229
0
        }
230
231
15.6k
        buf = nbuf;
232
15.6k
    }
233
207k
}
234
235
207k
char *avahi_strdup_printf(const char *fmt, ... ) {
236
207k
    char *s;
237
207k
    va_list ap;
238
239
207k
    assert(fmt);
240
241
207k
    va_start(ap, fmt);
242
207k
    s = avahi_strdup_vprintf(fmt, ap);
243
207k
    va_end(ap);
244
245
207k
    return s;
246
207k
}
247
248
4.78k
void *avahi_memdup(const void *s, size_t l) {
249
4.78k
    void *p;
250
4.78k
    assert(s);
251
252
4.78k
    if (!(p = avahi_malloc(l)))
253
0
        return NULL;
254
255
4.78k
    memcpy(p, s, l);
256
4.78k
    return p;
257
4.78k
}