Coverage Report

Created: 2025-07-12 06:30

/src/util-linux/include/xalloc.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * No copyright is claimed.  This code is in the public domain; do with
3
 * it what you wish.
4
 *
5
 * Authors 2010 Davidlohr Bueso <dave@gnu.org>
6
 *         2010-2025 Karel Zak <kzak@redhat.com>
7
 *
8
 * General memory allocation wrappers for malloc, realloc, calloc and strdup
9
 */
10
11
#ifndef UTIL_LINUX_XALLOC_H
12
#define UTIL_LINUX_XALLOC_H
13
14
#include <stdio.h>
15
#include <stdlib.h>
16
#include <string.h>
17
18
#include "c.h"
19
#include "strutils.h"
20
21
#ifndef XALLOC_EXIT_CODE
22
0
# define XALLOC_EXIT_CODE EXIT_FAILURE
23
#endif
24
25
static inline
26
__ul_alloc_size(1)
27
__ul_returns_nonnull
28
void *xmalloc(const size_t size)
29
21.8k
{
30
21.8k
  void *ret = malloc(size);
31
32
21.8k
  if (!ret && size)
33
0
    err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
34
21.8k
  return ret;
35
21.8k
}
last.c:xmalloc
Line
Count
Source
29
21.8k
{
30
21.8k
  void *ret = malloc(size);
31
32
21.8k
  if (!ret && size)
33
0
    err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
34
21.8k
  return ret;
35
21.8k
}
Unexecuted instantiation: fuzz.c:xmalloc
36
37
static inline
38
__ul_alloc_size(2)
39
__ul_returns_nonnull
40
void *xrealloc(void *ptr, const size_t size)
41
0
{
42
0
  void *ret = realloc(ptr, size);
43
0
44
0
  if (!ret && size)
45
0
    err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
46
0
  return ret;
47
0
}
Unexecuted instantiation: last.c:xrealloc
Unexecuted instantiation: fuzz.c:xrealloc
48
49
static inline
50
__ul_calloc_size(2, 3)
51
__ul_returns_nonnull
52
void *xreallocarray(void *ptr, const size_t nelems, const size_t size)
53
0
{
54
0
  void *ret = reallocarray(ptr, nelems, size);
55
0
56
0
  if (!ret && size && nelems)
57
0
    err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
58
0
  return ret;
59
0
}
Unexecuted instantiation: last.c:xreallocarray
Unexecuted instantiation: fuzz.c:xreallocarray
60
61
static inline
62
__ul_calloc_size(1, 2)
63
__ul_returns_nonnull
64
void *xcalloc(const size_t nelems, const size_t size)
65
0
{
66
0
  void *ret = calloc(nelems, size);
67
0
68
0
  if (!ret && size && nelems)
69
0
    err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
70
0
  return ret;
71
0
}
Unexecuted instantiation: last.c:xcalloc
Unexecuted instantiation: fuzz.c:xcalloc
72
73
static inline
74
__attribute__((warn_unused_result))
75
__ul_alloc_size(2)
76
__ul_returns_nonnull
77
void *xmemdup(const void *ptr, size_t size)
78
0
{
79
0
  void *ret = xmalloc(size);
80
0
81
0
  memcpy(ret, ptr, size);
82
0
  return ret;
83
0
}
Unexecuted instantiation: last.c:xmemdup
Unexecuted instantiation: fuzz.c:xmemdup
84
85
static inline
86
__attribute__((warn_unused_result))
87
__ul_returns_nonnull
88
char *xstrdup(const char *str)
89
1.15k
{
90
1.15k
  char *ret;
91
92
1.15k
  assert(str);
93
1.15k
  ret = strdup(str);
94
1.15k
  if (!ret)
95
0
    err(XALLOC_EXIT_CODE, "cannot duplicate string");
96
1.15k
  return ret;
97
1.15k
}
last.c:xstrdup
Line
Count
Source
89
1.15k
{
90
1.15k
  char *ret;
91
92
1.15k
  assert(str);
93
1.15k
  ret = strdup(str);
94
1.15k
  if (!ret)
95
0
    err(XALLOC_EXIT_CODE, "cannot duplicate string");
96
1.15k
  return ret;
97
1.15k
}
Unexecuted instantiation: fuzz.c:xstrdup
98
99
static inline
100
__attribute__((warn_unused_result))
101
__ul_returns_nonnull
102
char *xstrndup(const char *str, size_t size)
103
0
{
104
0
  char *ret;
105
0
106
0
  assert(str);
107
0
  ret = strndup(str, size);
108
0
  if (!ret)
109
0
    err(XALLOC_EXIT_CODE, "cannot duplicate string");
110
0
  return ret;
111
0
}
Unexecuted instantiation: last.c:xstrndup
Unexecuted instantiation: fuzz.c:xstrndup
112
113
114
static inline
115
__attribute__((__format__(printf, 2, 3)))
116
int xasprintf(char **strp, const char *fmt, ...)
117
0
{
118
0
  int ret;
119
0
  va_list args;
120
0
121
0
  va_start(args, fmt);
122
0
  ret = vasprintf(&(*strp), fmt, args);
123
0
  va_end(args);
124
0
  if (ret < 0)
125
0
    err(XALLOC_EXIT_CODE, "cannot allocate string");
126
0
  return ret;
127
0
}
Unexecuted instantiation: last.c:xasprintf
Unexecuted instantiation: fuzz.c:xasprintf
128
129
static inline
130
__attribute__((__format__(printf, 2, 0)))
131
int xvasprintf(char **strp, const char *fmt, va_list ap)
132
0
{
133
0
  int ret = vasprintf(&(*strp), fmt, ap);
134
0
135
0
  if (ret < 0)
136
0
    err(XALLOC_EXIT_CODE, "cannot allocate string");
137
0
  return ret;
138
0
}
Unexecuted instantiation: last.c:xvasprintf
Unexecuted instantiation: fuzz.c:xvasprintf
139
140
static inline void xstrappend(char **a, const char *b)
141
0
{
142
0
  if (ul_strappend(a, b) < 0)
143
0
    err(XALLOC_EXIT_CODE, "cannot allocate string");
144
0
}
Unexecuted instantiation: last.c:xstrappend
Unexecuted instantiation: fuzz.c:xstrappend
145
146
static inline void xstrputc(char **a, char c)
147
0
{
148
0
  char b[] = {c, '\0'};
149
0
  xstrappend(a, b);
150
0
}
Unexecuted instantiation: last.c:xstrputc
Unexecuted instantiation: fuzz.c:xstrputc
151
152
static inline
153
__attribute__((__format__(printf, 2, 0)))
154
int xstrvfappend(char **a, const char *format, va_list ap)
155
0
{
156
0
  int ret = ul_strvfappend(a, format, ap);
157
0
158
0
  if (ret < 0)
159
0
    err(XALLOC_EXIT_CODE, "cannot allocate string");
160
0
  return ret;
161
0
162
0
}
Unexecuted instantiation: last.c:xstrvfappend
Unexecuted instantiation: fuzz.c:xstrvfappend
163
164
static inline
165
__attribute__ ((__format__ (__printf__, 2, 3)))
166
int xstrfappend(char **a, const char *format, ...)
167
0
{
168
0
  va_list ap;
169
0
  int ret;
170
0
171
0
  va_start(ap, format);
172
0
  ret = xstrvfappend(a, format, ap);
173
0
  va_end(ap);
174
0
175
0
  return ret;
176
0
}
Unexecuted instantiation: last.c:xstrfappend
Unexecuted instantiation: fuzz.c:xstrfappend
177
178
static inline
179
__attribute__((warn_unused_result))
180
char *xgethostname(void)
181
0
{
182
0
  char *name;
183
0
  size_t sz = get_hostname_max() + 1;
184
0
185
0
  name = xmalloc(sizeof(char) * sz);
186
0
  if (gethostname(name, sz) != 0) {
187
0
    free(name);
188
0
    return NULL;
189
0
  }
190
0
  name[sz - 1] = '\0';
191
0
  return name;
192
0
}
Unexecuted instantiation: last.c:xgethostname
Unexecuted instantiation: fuzz.c:xgethostname
193
194
static inline
195
__attribute__((warn_unused_result))
196
char *xgethosturi(const char *proto)
197
0
{
198
0
  char *n = xgethostname();
199
0
  char *uri = NULL;
200
0
201
0
  if (!proto)
202
0
    proto = "file://";
203
0
  if (!n)
204
0
    return xstrdup(proto);
205
0
206
0
  xasprintf(&uri, "%s%s", proto, n);
207
0
  free(n);
208
0
  return uri;
209
0
}
Unexecuted instantiation: last.c:xgethosturi
Unexecuted instantiation: fuzz.c:xgethosturi
210
211
#endif