forked from th-otto/wifi_da
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalloc.c
More file actions
134 lines (107 loc) · 3.16 KB
/
malloc.c
File metadata and controls
134 lines (107 loc) · 3.16 KB
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
/*
* Copyright (c) 2024 Thorsten Otto
* based on code
* Copyright (c) 2020-2022 joshua stein <jcs@jcs.org>
* Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdarg.h>
#include <string.h>
#include "wi-fi.h"
#include "util.h"
/* strangely, this is defined only in stdint.h, not limits.h */
#ifndef SIZE_MAX
#ifdef __SIZE_MAX__
#define SIZE_MAX __SIZE_MAX__
#else
#define SIZE_MAX 4294967295UL
#endif
#endif
/*
* Define to enable malloc debugging which creates an allocation larger
* than the requested size, then embeds the allocated size and 2 canary
* bytes before and after the allocation. On xfree(), the canary bytes
* are verified and if any are not correct, the program panics.
*/
/* #define MALLOC_DEBUG 1 */
/*
* Memory functions
*/
void *xmalloc(size_t size)
{
#ifdef MALLOC_DEBUG
void *ptr;
unsigned char *cptr;
#endif
if (size == 0)
panic("xmalloc: zero size");
#ifdef MALLOC_DEBUG
ptr = malloc(size + 8);
cptr = (unsigned char *) ptr;
cptr[0] = 0xff;
cptr[1] = 0xff;
cptr[2] = (size >> 24) & 0xff;
cptr[3] = (size >> 16) & 0xff;
cptr[4] = (size >> 8) & 0xff;
cptr[5] = size & 0xff;
cptr[6 + size] = 0xff;
cptr[6 + size + 1] = 0xff;
return cptr + 6;
#else
return malloc(size);
#endif
}
void xfree(void *ptrptr)
{
void **addr = (void **) ptrptr;
#ifdef MALLOC_DEBUG
size_t size;
unsigned char *cptr;
#endif
void *ptr;
if (ptrptr == NULL)
panic("xfree(NULL)");
ptr = *addr;
if (ptr == NULL)
panic("xfree(&NULL) likely a double-free");
#ifdef MALLOC_DEBUG
cptr = (unsigned char *) ptr - 6;
if (cptr[0] != 0xff || cptr[1] != 0xff)
panic("xfree() pre-buf canary dead");
size = ((unsigned long) (cptr[2]) << 24) |
((unsigned long) (cptr[3]) << 16) | ((unsigned long) (cptr[4]) << 8) | (unsigned long) (cptr[5]);
if (cptr[6 + size] != 0xff || cptr[6 + size + 1] != 0xff)
panic("xfree() post-buf canary dead");
free(cptr);
#else
free(ptr);
#endif
*addr = NULL;
}
void *xmalloczero(size_t size)
{
void *ptr;
ptr = xmalloc(size);
if (ptr != NULL)
memset(ptr, 0, size);
return ptr;
}
void *xcalloc(size_t nmemb, size_t size)
{
if (nmemb > 0 && SIZE_MAX / nmemb < size)
panic("xcalloc(%lu, %lu) overflow", nmemb, size);
return xmalloczero(nmemb * size);
}