diff --git a/thirdparty/vhacd/inc/btScalar.h b/thirdparty/vhacd/inc/btScalar.h index 404bcbcfe1..db00a3c428 100644 --- a/thirdparty/vhacd/inc/btScalar.h +++ b/thirdparty/vhacd/inc/btScalar.h @@ -537,6 +537,27 @@ struct btTypedObject { } }; +// Cherry-picked from Bullet 2.88 to fix GH-27926 +///align a pointer to the provided alignment, upwards +template +T *btAlignPointer(T *unalignedPtr, size_t alignment) +{ + struct btConvertPointerSizeT + { + union { + T *ptr; + size_t integer; + }; + }; + btConvertPointerSizeT converter; + + const size_t bit_mask = ~(alignment - 1); + converter.ptr = unalignedPtr; + converter.integer += alignment - 1; + converter.integer &= bit_mask; + return converter.ptr; +} + }; // namespace VHACD #endif //BT_SCALAR_H diff --git a/thirdparty/vhacd/src/btAlignedAllocator.cpp b/thirdparty/vhacd/src/btAlignedAllocator.cpp index bbb8baa107..4d7f4b1b2f 100644 --- a/thirdparty/vhacd/src/btAlignedAllocator.cpp +++ b/thirdparty/vhacd/src/btAlignedAllocator.cpp @@ -70,8 +70,10 @@ static inline void* btAlignedAllocDefault(size_t size, int32_t alignment) real = (char*)sAllocFunc(size + sizeof(void*) + (alignment - 1)); if (real) { - offset = (alignment - (unsigned long)(real + sizeof(void*))) & (alignment - 1); - ret = (void*)((real + sizeof(void*)) + offset); + // Synced with Bullet 2.88 to fix GH-27926 + //offset = (alignment - (unsigned long)(real + sizeof(void*))) & (alignment - 1); + //ret = (void*)((real + sizeof(void*)) + offset); + ret = btAlignPointer(real + sizeof(void *), alignment); *((void**)(ret)-1) = (void*)(real); } else {