Debian Codegraph @

src/wrapped.c

raw
1 /**
2 * This file is part of the Detox package.
3 *
4 * Copyright (c) Doug Harple <[email protected]>
5 *
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
8 */
9
10 #include "config.h"
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
16
17 #include "wrapped.h"
18
19 #ifdef SUPPORT_COVERAGE
20 int wrapped_malloc_failure = 0;
21 int wrapped_strdup_failure = 0;
22 #endif
23
24 void *wrapped_malloc(size_t size)
25 {
26 void *ret;
27 int err;
28
29 ret = malloc(size);
30
31 #ifdef SUPPORT_COVERAGE
32 if (wrapped_malloc_failure != 0) {
33 free(ret);
34 ret = NULL;
35 }
36 #endif
37
38 if (ret == NULL) {
39 err = errno;
40 fprintf(stderr, "detox: out of memory: %s\n", strerror(err));
41 exit(EXIT_FAILURE);
42 }
43
44 return ret;
45 }
46
47 char *wrapped_strdup(const char *s)
48 {
49 char *ret;
50 int err;
51
52 ret = strdup(s);
53
54 #ifdef SUPPORT_COVERAGE
55 if (wrapped_strdup_failure != 0) {
56 free(ret);
57 ret = NULL;
58 }
59 #endif
60
61 if (ret == NULL) {
62 err = errno;
63 fprintf(stderr, "detox: out of memory: %s\n", strerror(err));
64 exit(EXIT_FAILURE);
65 }
66
67 return ret;
68 }