-
Notifications
You must be signed in to change notification settings - Fork 27
/
errf.c
327 lines (291 loc) · 7.47 KB
/
errf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2019, Joyent Inc
* Author: Alex Wilson <alex.wilson@joyent.com>
*/
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#include "errf.h"
struct errf {
struct errf *errf_cause;
int errf_errno;
char errf_name[128];
char errf_message[256];
char errf_function[64];
char errf_file[64];
uint errf_line;
};
/*
* Note that this is not intended to be an exhaustive list of errno macros.
*
* Sticking mostly to portable ones and ones where the strerror() might be
* confusing is fine.
*/
const char *
errno_to_macro(int eno)
{
switch (eno) {
case EPERM: return ("EPERM");
case ENOENT: return ("ENOENT");
case ESRCH: return ("ESRCH");
case EINTR: return ("EINTR");
case EIO: return ("EIO");
case ENXIO: return ("ENXIO");
case E2BIG: return ("E2BIG");
case ENOEXEC: return ("ENOEXEC");
case EBADF: return ("EBADF");
case ECHILD: return ("ECHILD");
case EAGAIN: return ("EAGAIN");
case ENOMEM: return ("ENOMEM");
case EACCES: return ("EACCES");
case EFAULT: return ("EFAULT");
case ENOTBLK: return ("ENOTBLK");
case EBUSY: return ("EBUSY");
case EEXIST: return ("EEXIST");
case EXDEV: return ("EXDEV");
case ENODEV: return ("ENODEV");
case ENOTDIR: return ("ENOTDIR");
case EISDIR: return ("EISDIR");
case EINVAL: return ("EINVAL");
case ENFILE: return ("ENFILE");
case EMFILE: return ("EMFILE");
case ENOTTY: return ("ENOTTY");
case ETXTBSY: return ("ETXTBSY");
case EFBIG: return ("EFBIG");
case ENOSPC: return ("ENOSPC");
case ESPIPE: return ("ESPIPE");
case EROFS: return ("EROFS");
case EMLINK: return ("EMLINK");
case EPIPE: return ("EPIPE");
case EDOM: return ("EDOM");
case ERANGE: return ("ERANGE");
default: return (NULL);
}
}
struct errf errf_ok = {
.errf_name = "NoError",
.errf_message = "warnfx/errfx() called on a non-error",
.errf_file = "erf.c",
.errf_line = __LINE__
};
struct errf errf_nomem = {
.errf_errno = ENOMEM,
.errf_name = "OutOfMemoryError",
.errf_message = "Process failed to allocate new memory",
.errf_file = "erf.c",
.errf_line = __LINE__
};
struct errf *ERRF_NOMEM = &errf_nomem;
const char *
errf_name(const struct errf *e)
{
return (e->errf_name);
}
const char *
errf_message(const struct errf *e)
{
return (e->errf_message);
}
int
errf_errno(const struct errf *e)
{
return (e->errf_errno);
}
const char *
errf_function(const struct errf *e)
{
return (e->errf_function);
}
const char *
errf_file(const struct errf *e)
{
return (e->errf_file);
}
uint
errf_line(const struct errf *e)
{
return (e->errf_line);
}
struct errf *
errf_cause(const struct errf *e)
{
return (e->errf_cause);
}
struct errf *
_errf(const char *name, struct errf *cause, const char *func, const char *file,
uint line, const char *fmt, ...)
{
struct errf *e;
int wrote;
va_list ap;
e = calloc(1, sizeof (struct errf));
if (e == NULL)
return (ERRF_NOMEM);
strlcpy(e->errf_name, name, sizeof (e->errf_name));
e->errf_cause = cause;
strlcpy(e->errf_file, file, sizeof (e->errf_file));
e->errf_line = line;
strlcpy(e->errf_function, func, sizeof (e->errf_function));
va_start(ap, fmt);
wrote = vsnprintf(e->errf_message, sizeof (e->errf_message), fmt, ap);
if (wrote < 0) {
int eno = errno;
const char *macro = errno_to_macro(errno);
e->errf_message[0] = '\0';
wrote = snprintf(e->errf_message, sizeof (e->errf_message),
"vsnprintf returned errno %d (%s): %s", eno, macro,
strerror(eno));
if (wrote < 0) {
e->errf_message[0] = '\0';
strlcpy(e->errf_message, "<vsnprintf failed>",
sizeof (e->errf_message));
}
}
va_end(ap);
if (wrote > 0 && (size_t)wrote >= sizeof (e->errf_message))
e->errf_message[sizeof (e->errf_message) - 1] = '\0';
return (e);
}
struct errf *
_errfno(const char *enofunc, int eno, const char *func, const char *file,
uint line, const char *fmt, ...)
{
struct errf *e;
char *p;
int wrote;
va_list ap;
const char *macro;
macro = errno_to_macro(eno);
e = calloc(1, sizeof (struct errf));
if (e == NULL)
return (ERRF_NOMEM);
strlcpy(e->errf_name, macro ? macro : "SystemError",
sizeof (e->errf_name));
strlcpy(e->errf_file, file, sizeof (e->errf_file));
e->errf_line = line;
e->errf_errno = eno;
strlcpy(e->errf_function, func, sizeof (e->errf_function));
wrote = snprintf(e->errf_message, sizeof (e->errf_message),
"%s returned errno %d (%s): %s%s", enofunc, eno, macro,
strerror(eno), fmt ? ": " : "");
if (wrote < 0) {
e->errf_message[0] = '\0';
strlcpy(e->errf_message, "<vsnprintf failed>",
sizeof (e->errf_message));
return (e);
} else if ((size_t)wrote >= sizeof (e->errf_message)) {
e->errf_message[sizeof (e->errf_message) - 1] = '\0';
return (e);
}
if (fmt != NULL) {
int nwrote;
p = &e->errf_message[wrote];
va_start(ap, fmt);
nwrote = vsnprintf(p, sizeof (e->errf_message) - wrote,
fmt, ap);
if (nwrote < 0) {
wrote = -1;
} else {
const uint sum = (uint)wrote + (uint)nwrote;
if (sum < wrote || sum > INT_MAX) {
e->errf_message[0] = '\0';
strlcpy(e->errf_message, "<fmt overflow>",
sizeof (e->errf_message));
return (e);
}
wrote = (int)sum;
}
va_end(ap);
}
if (wrote > 0 && (size_t)wrote >= sizeof (e->errf_message))
e->errf_message[sizeof (e->errf_message) - 1] = '\0';
return (e);
}
static void
vperrf(const struct errf *etop, const char *type, const char *fmt, va_list args)
{
const struct errf *e;
if (etop == NULL) {
vperrf(&errf_ok, type, fmt, args);
return;
}
fprintf(stderr, "%s: %s", getprogname(), type);
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
for (e = etop; e != NULL; e = e->errf_cause) {
fprintf(stderr, " Caused by %s: %s\n", e->errf_name,
e->errf_message);
fprintf(stderr, " in %s() at %s:%u\n", e->errf_function,
e->errf_file, e->errf_line);
}
}
void
warnfx(const struct errf *etop, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vperrf(etop, "warning: ", fmt, ap);
va_end(ap);
}
void
errfx(int status, const struct errf *etop, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vperrf(etop, "", fmt, ap);
va_end(ap);
exit(status);
}
boolean_t
errf_caused_by(const struct errf *e, const char *name)
{
for (; e != NULL; e = e->errf_cause) {
if (strcmp(name, e->errf_name) == 0)
return (B_TRUE);
}
return (B_FALSE);
}
void
errf_free(struct errf *ep)
{
while (ep != NULL) {
struct errf *tofree = ep;
ep = ep->errf_cause;
if (tofree != ERRF_NOMEM)
free(tofree);
}
}
#if defined(__CPROVER) && __CPROVER_MAIN == __FILE_errf_c
#include <assert.h>
int
main(int argc, char *argv[])
{
errf_t *err;
err = errf("Testing", NULL, "what is the format: %d", 5);
assert(err != NULL);
__CPROVER_assume(err != ERRF_NOMEM);
assert(strcmp(errf_name(err), "Testing") == 0);
assert(errf_caused_by(err, "Testing") == B_TRUE);
err = errf("Test", err, "something else");
assert(err != NULL);
__CPROVER_assume(err != ERRF_NOMEM);
assert(strcmp(errf_name(err), "Test") == 0);
assert(errf_caused_by(err, "Test") == B_TRUE);
assert(errf_caused_by(err, "Testing") == B_TRUE);
assert(errf_caused_by(err, "Else") == B_FALSE);
errf_free(err);
err = errfno("test", EPERM, "what");
assert(err != NULL);
__CPROVER_assume(err != ERRF_NOMEM);
assert(strcmp(errf_name(err), "EPERM") == 0);
assert(errf_errno(err) == EPERM);
errf_free(err);
}
#endif