@@ -149,30 +149,28 @@ class BumpPtrAllocatorImpl
149149 // Keep track of how many bytes we've allocated.
150150 BytesAllocated += Size;
151151
152- uintptr_t AlignedPtr = alignAddr (CurPtr, Alignment);
152+ size_t Adjustment = offsetToAlignedAddr (CurPtr, Alignment);
153+ assert (Adjustment + Size >= Size && " Adjustment + Size must not overflow" );
153154
154155 size_t SizeToAllocate = Size;
155156#if LLVM_ADDRESS_SANITIZER_BUILD
156157 // Add trailing bytes as a "red zone" under ASan.
157158 SizeToAllocate += RedZoneSize;
158159#endif
159160
160- uintptr_t AllocEndPtr = AlignedPtr + SizeToAllocate;
161- assert (AllocEndPtr >= uintptr_t (CurPtr) &&
162- " Alignment + Size must not overflow" );
163-
164161 // Check if we have enough space.
165- if (LLVM_LIKELY (AllocEndPtr <= uintptr_t (End)
162+ if (LLVM_LIKELY (Adjustment + SizeToAllocate <= size_t (End - CurPtr )
166163 // We can't return nullptr even for a zero-sized allocation!
167164 && CurPtr != nullptr )) {
168- CurPtr = reinterpret_cast <char *>(AllocEndPtr);
165+ char *AlignedPtr = CurPtr + Adjustment;
166+ CurPtr = AlignedPtr + SizeToAllocate;
169167 // Update the allocation point of this memory block in MemorySanitizer.
170168 // Without this, MemorySanitizer messages for values originated from here
171169 // will point to the allocation of the entire slab.
172170 __msan_allocated_memory (AlignedPtr, Size);
173171 // Similarly, tell ASan about this space.
174172 __asan_unpoison_memory_region (AlignedPtr, Size);
175- return reinterpret_cast < char *>( AlignedPtr) ;
173+ return AlignedPtr;
176174 }
177175
178176 return AllocateSlow (Size, SizeToAllocate, Alignment);
0 commit comments