Skip to content

src: remove UncheckedMalloc(0) workaround

Unlike UncheckedRealloc(p, 0), which returns a nullptr, both UncheckedMalloc(0) and UncheckedCalloc(0) currently perform fake allocations to return a non-nullptr.

Assuming that UncheckedMalloc(0) returns a non-nullptr is non-standard and we use other allocators as well (e.g., OPENSSL_malloc) that do not guarantee this behavior. It is the caller's responsibility to check that size != 0 implies UncheckedMalloc(size) != nullptr, and that's exactly what the checked variants (Malloc etc.) already do.

The current behavior is also inconsistent with UncheckedRealloc(), which always returns a nullptr when the size is 0, and with the documentation in src/README.md as well as with multiple comments in the source code.

https://github.com/nodejs/node/blob/0917626b96c2229ace91b55f72bc3faf152e74e9/src/README.md?plain=1#L954-L955

https://github.com/nodejs/node/blob/e62f6ce630c3ea9d9484d14bb892cb6836b250d4/src/util.h#L69-L78

https://github.com/nodejs/node/blob/e62f6ce630c3ea9d9484d14bb892cb6836b250d4/src/util-inl.h#L334-L340


This changes UncheckedMalloc(), UncheckedCalloc(), and UncheckedRealloc() to always return a nullptr when the size is 0 instead of doing fake allocations in UncheckedMalloc() and UncheckedCalloc() while returning a nullptr from UncheckedRealloc(). This is consistent with existing documentation and comments.


Refs: https://github.com/nodejs/node/issues/8571 Refs: https://github.com/nodejs/node/pull/8572

Merge request reports

Loading