add explicit null checks to system_allocator malloc/realloc
This commit is contained in:
parent
f69698d734
commit
0653a9e48a
1 changed files with 10 additions and 2 deletions
|
|
@ -7,6 +7,9 @@
|
||||||
static void* system_alloc(HAllocator *allocator, size_t size) {
|
static void* system_alloc(HAllocator *allocator, size_t size) {
|
||||||
|
|
||||||
void* ptr = malloc(size + sizeof(size_t));
|
void* ptr = malloc(size + sizeof(size_t));
|
||||||
|
if (!ptr) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
#ifdef DEBUG__MEMFILL
|
#ifdef DEBUG__MEMFILL
|
||||||
memset(ptr, DEBUG__MEMFILL, size + sizeof(size_t));
|
memset(ptr, DEBUG__MEMFILL, size + sizeof(size_t));
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -15,9 +18,13 @@ static void* system_alloc(HAllocator *allocator, size_t size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static void* system_realloc(HAllocator *allocator, void* ptr, size_t size) {
|
static void* system_realloc(HAllocator *allocator, void* ptr, size_t size) {
|
||||||
if (ptr == NULL)
|
if (!ptr) {
|
||||||
return system_alloc(allocator, size);
|
return system_alloc(allocator, size);
|
||||||
|
}
|
||||||
ptr = realloc(ptr - sizeof(size_t), size + sizeof(size_t));
|
ptr = realloc(ptr - sizeof(size_t), size + sizeof(size_t));
|
||||||
|
if (!ptr) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
*(size_t*)ptr = size;
|
*(size_t*)ptr = size;
|
||||||
#ifdef DEBUG__MEMFILL
|
#ifdef DEBUG__MEMFILL
|
||||||
size_t old_size = *(size_t*)ptr;
|
size_t old_size = *(size_t*)ptr;
|
||||||
|
|
@ -28,8 +35,9 @@ static void* system_realloc(HAllocator *allocator, void* ptr, size_t size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static void system_free(HAllocator *allocator, void* ptr) {
|
static void system_free(HAllocator *allocator, void* ptr) {
|
||||||
if (ptr != NULL)
|
if (ptr) {
|
||||||
free(ptr - sizeof(size_t));
|
free(ptr - sizeof(size_t));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
HAllocator system_allocator = {
|
HAllocator system_allocator = {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue