operator delete should check for null pointer before deallocating

It is a pervasive belief that using "delete" with a null pointer is safe, so our custom delete operators should also handle this case correctly.

This may fix regressions introduced by #934
This commit is contained in:
rdb 2020-05-05 17:59:48 +02:00
parent 316d254c64
commit 9159fc1029
3 changed files with 17 additions and 7 deletions

View File

@ -1124,7 +1124,9 @@ operator new(size_t size) {
*/
INLINE void DCPacker::StackElement::
operator delete(void *ptr) {
StackElement *obj = (StackElement *)ptr;
obj->_next = _deleted_chain;
_deleted_chain = obj;
if (ptr != nullptr) {
StackElement *obj = (StackElement *)ptr;
obj->_next = _deleted_chain;
_deleted_chain = obj;
}
}

View File

@ -85,7 +85,9 @@ public:
return ptr; \
} \
inline void operator delete(void *ptr) { \
StaticDeletedChain< Type >::deallocate((Type *)ptr, get_type_handle(Type)); \
if (ptr != nullptr) { \
StaticDeletedChain< Type >::deallocate((Type *)ptr, get_type_handle(Type)); \
} \
} \
inline void operator delete(void *, void *) { \
} \
@ -104,7 +106,9 @@ public:
return ptr; \
} \
inline void operator delete(void *ptr) { \
_deleted_chain.deallocate((Type *)ptr, get_type_handle(Type)); \
if (ptr != nullptr) { \
_deleted_chain.deallocate((Type *)ptr, get_type_handle(Type)); \
} \
} \
inline void operator delete(void *, void *) { \
} \

View File

@ -32,7 +32,9 @@
return ptr; \
} \
inline void operator delete(void *ptr) { \
PANDA_FREE_SINGLE(ptr); \
if (ptr != nullptr) { \
PANDA_FREE_SINGLE(ptr); \
} \
} \
inline void operator delete(void *, void *) { \
} \
@ -44,7 +46,9 @@
return ptr; \
} \
inline void operator delete[](void *ptr) { \
PANDA_FREE_ARRAY(ptr); \
if (ptr != nullptr) { \
PANDA_FREE_ARRAY(ptr); \
} \
} \
inline void operator delete[](void *, void *) { \
}