[llvm] r216973 - BumpPtrAllocator: use uintptr_t when aligning addresses to avoid undefined behaviour

Nick Kledzik kledzik at apple.com
Tue Sep 2 19:13:20 PDT 2014


On Sep 2, 2014, at 2:51 PM, Hans Wennborg <hans at hanshq.net> wrote:

> Author: hans
> Date: Tue Sep  2 16:51:35 2014
> New Revision: 216973
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=216973&view=rev
> Log:
> BumpPtrAllocator: use uintptr_t when aligning addresses to avoid undefined behaviour
> 
> In theory, alignPtr() could push a pointer beyond the end of the current slab, making
> comparisons with that pointer undefined behaviour. Use an integer type to avoid this.
> 
> Modified:
>    llvm/trunk/include/llvm/Support/Allocator.h
>    llvm/trunk/include/llvm/Support/MathExtras.h
>    llvm/trunk/unittests/Support/AllocatorTest.cpp
> 
> Modified: llvm/trunk/include/llvm/Support/Allocator.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Allocator.h?rev=216973&r1=216972&r2=216973&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/Support/Allocator.h (original)
> +++ llvm/trunk/include/llvm/Support/Allocator.h Tue Sep  2 16:51:35 2014
> @@ -210,16 +210,17 @@ public:
>     BytesAllocated += Size;
> 
>     // Allocate the aligned space, going forwards from CurPtr.
> -    char *Ptr = alignPtr(CurPtr, Alignment);
> +    uintptr_t AlignedAddr = alignAddr(CurPtr, Alignment);
> 
...

>     // If Size is really big, allocate a separate slab for it.
> @@ -228,19 +229,22 @@ public:
>       void *NewSlab = Allocator.Allocate(PaddedSize, 0);
>       CustomSizedSlabs.push_back(std::make_pair(NewSlab, PaddedSize));
> 
> -      Ptr = alignPtr((char *)NewSlab, Alignment);
> -      assert((uintptr_t)Ptr + Size <= (uintptr_t)NewSlab + PaddedSize);
> -      __msan_allocated_memory(Ptr, Size);
> -      return Ptr;
> +      uintptr_t AlignedAddr = alignAddr(NewSlab, Alignment);

This causes warnings for me because you have two local variables named “AlignedAddr”.   [-Wshadow]

-Nick





More information about the llvm-commits mailing list