[all-commits] [llvm/llvm-project] ba0d50: [Support] Fix UB in BumpPtrAllocator when first al...

Sam McCall via All-commits all-commits at lists.llvm.org
Thu May 5 23:57:52 PDT 2022

  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: ba0d50ad7ec664bfc79f691bfd358d6a0d815505
  Author: Sam McCall <sam.mccall at gmail.com>
  Date:   2022-05-06 (Fri, 06 May 2022)

  Changed paths:
    M llvm/include/llvm/Support/Allocator.h
    M llvm/unittests/Support/AllocatorTest.cpp

  Log Message:
  [Support] Fix UB in BumpPtrAllocator when first allocation is zero.

BumpPtrAllocator::Allocate() is marked __attribute__((returns_nonnull)) when the
compiler supports it, which makes it UB to return null.

When there have been no allocations yet, the current slab is [nullptr, nullptr).
A zero-sized allocation fits in this range, and so Allocate(0, 1) returns null.

There's no explicit docs whether Allocate(0) is valid. I think we have to assume
that it is:
 - the implementation tries to support it (e.g. >= tests instead of >)
 - malloc(0) is allowed
 - requiring each callsite to do a check is bug-prone
 - I found real LLVM code that makes zero-sized allocations

Differential Revision: https://reviews.llvm.org/D125040

More information about the All-commits mailing list