[PATCH] D63668: [Support] Improve zero-size allocation with safe_malloc, etc.

Hubert Tong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 23 21:27:16 PDT 2019


hubert.reinterpretcast added a comment.

> I don't believe this is the right way to go, however. As you point out malloc(0) is undefined behavior

It isn't. It is implementation-defined whether or not it returns a null pointer. In either case, it returns a value on which `free` can be safely called.

> I would think that asking for 0 bytes would be indicative of a bug that can be found in testing debug builds. Personally, `assert(Sz && "Tried to allocate 0")` seems like it would suffice, but I see why you went in this direction.

`malloc` and friends are supposed to succeed when asked for 0 bytes except when there is insufficient space. The caller is within its rights to ask for 0 bytes and expect a value on which it may call `free` except when there is insufficient space.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D63668/new/

https://reviews.llvm.org/D63668





More information about the llvm-commits mailing list