Skip to content

Commit

Permalink
operator delete should check for null pointer before deallocating
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rdb committed May 5, 2020
1 parent 316d254 commit 9159fc1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
8 changes: 5 additions & 3 deletions direct/src/dcparser/dcPacker.I
Expand Up @@ -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;
}
}
8 changes: 6 additions & 2 deletions dtool/src/dtoolbase/deletedChain.h
Expand Up @@ -85,7 +85,9 @@ class StaticDeletedChain {
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 *) { \
} \
Expand All @@ -104,7 +106,9 @@ class StaticDeletedChain {
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 *) { \
} \
Expand Down
8 changes: 6 additions & 2 deletions dtool/src/dtoolbase/memoryBase.h
Expand Up @@ -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 *) { \
} \
Expand All @@ -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 *) { \
}
Expand Down

0 comments on commit 9159fc1

Please sign in to comment.