Merge pull request #1743 from pi-hole/new/zero_after_free
NULL after free()
This commit is contained in:
commit
ca0a3e50b8
|
@ -138,7 +138,7 @@
|
|||
// caused by insufficient memory or by code bugs (not properly dealing
|
||||
// with NULL pointers) much easier.
|
||||
#undef strdup // strdup() is a macro in itself, it needs special handling
|
||||
#define free(ptr) FTLfree(ptr, __FILE__, __FUNCTION__, __LINE__)
|
||||
#define free(ptr) FTLfree((void**)&ptr, __FILE__, __FUNCTION__, __LINE__)
|
||||
#define strdup(str_in) FTLstrdup(str_in, __FILE__, __FUNCTION__, __LINE__)
|
||||
#define calloc(numer_of_elements, element_size) FTLcalloc(numer_of_elements, element_size, __FILE__, __FUNCTION__, __LINE__)
|
||||
#define realloc(ptr, new_size) FTLrealloc(ptr, new_size, __FILE__, __FUNCTION__, __LINE__)
|
||||
|
|
|
@ -13,17 +13,26 @@
|
|||
#include "../log.h"
|
||||
|
||||
#undef free
|
||||
void FTLfree(void *ptr, const char *file, const char *func, const int line)
|
||||
void FTLfree(void **ptr, const char *file, const char *func, const int line)
|
||||
{
|
||||
// The free() function frees the memory space pointed to by ptr, which
|
||||
// must have been returned by a previous call to malloc(), calloc(), or
|
||||
// realloc(). Otherwise, or if free(ptr) has already been called before,
|
||||
// undefined behavior occurs. If ptr is NULL, no operation is performed.
|
||||
if(ptr == NULL)
|
||||
{
|
||||
log_warn("Trying to free NULL memory location in %s() (%s:%i)", func, file, line);
|
||||
return;
|
||||
}
|
||||
if(*ptr == NULL)
|
||||
{
|
||||
log_warn("Trying to free NULL pointer in %s() (%s:%i)", func, file, line);
|
||||
return;
|
||||
}
|
||||
|
||||
free(ptr);
|
||||
// Actually free the memory
|
||||
free(*ptr);
|
||||
|
||||
// Set the pointer to NULL
|
||||
*ptr = NULL;
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
char *FTLstrdup(const char *src, const char *file, const char *func, const int line) __attribute__((malloc));
|
||||
void *FTLcalloc(size_t n, size_t size, const char *file, const char *func, const int line) __attribute__((malloc)) __attribute__((alloc_size(1,2)));
|
||||
void *FTLrealloc(void *ptr_in, size_t size, const char *file, const char *func, const int line) __attribute__((alloc_size(2)));
|
||||
void FTLfree(void *ptr, const char*file, const char *func, const int line);
|
||||
void FTLfree(void **ptr, const char*file, const char *func, const int line);
|
||||
int FTLfallocate(const int fd, const off_t offset, const off_t len, const char *file, const char *func, const int line);
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue