[PATCH] D51317: Keep BumpPtrAllocator::Allocate(0) from returning nullptr

Brent Royal-Gordon via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 28 02:28:37 PDT 2018


brentdax added a comment.

Zero-size allocations usually happen when an array or string happens to have zero elements. I'm trying to clean up UBSan failures in the Swift compiler, and there are several places where this happens there, such as when an array of code completions has no results <https://github.com/apple/swift/blob/master/lib/IDE/CodeCompletion.cpp#L1155>. We could guard every call like this with an if/else, or we could wrap llvm::BumpPtrAllocator with something that has the if/else statements, or we could change llvm::BumpPtrAllocator so that it handles zero-size allocations without breaking its own contract.

If we agree that the third approach is the right one, then the implementations I can think of are to allow nullptr returns (but then people might think we return null on error), allocate a byte even on size-zero allocations (probably adds a branch, plus it wastes a little memory), or some variation of this patch's solution (returning the same address on consecutive size-zero allocations). This implementation seemed like it would be the fastest and least invasive, so it's the one I chose, but I'm not the expert here.


Repository:
  rL LLVM

https://reviews.llvm.org/D51317





More information about the llvm-commits mailing list