From 8ea425d95d835f13e70700c116fe14b22ce347cc Mon Sep 17 00:00:00 2001
From: Jacques Lucke <jacques@blender.org>
Date: Wed, 21 Feb 2024 18:14:11 +0100
Subject: [PATCH] Fix #118402: enforce expected minimum alignment in
 MEM_CXX_CLASS_ALLOC_FUNCS

This `operator new` added in ecc3e78d787cce8a3f202e7de26575e2d47baea2
are only called if the alignment is greater than `__STDCPP_DEFAULT_NEW_ALIGNMENT__`.
This is generally 8 or 16 depending on the platform. `MEM_mallocN` does
guarantee 16 byte alignment currently (in fact it's usually not 16 byte aligned
because of `MemHead`). Now `MEM_mallocN_aligned` is used with the default
alignment, even if we don't know that the type does not require it.

An alternative would be to pass the alignment to `MEM_CXX_CLASS_ALLOC_FUNCS`,
but that would be more intrusive.

Pull Request: https://projects.blender.org/blender/blender/pulls/118568
---
 intern/guardedalloc/MEM_guardedalloc.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/intern/guardedalloc/MEM_guardedalloc.h b/intern/guardedalloc/MEM_guardedalloc.h
index ee4158af923..e142802e6c5 100644
--- a/intern/guardedalloc/MEM_guardedalloc.h
+++ b/intern/guardedalloc/MEM_guardedalloc.h
@@ -339,7 +339,7 @@ template<typename T> inline T *MEM_cnew(const char *allocation_name, const T &ot
    public: \
     void *operator new(size_t num_bytes) \
     { \
-      return MEM_mallocN(num_bytes, _id); \
+      return MEM_mallocN_aligned(num_bytes, __STDCPP_DEFAULT_NEW_ALIGNMENT__, _id); \
     } \
     void *operator new(size_t num_bytes, std::align_val_t alignment) \
     { \
@@ -353,7 +353,7 @@ template<typename T> inline T *MEM_cnew(const char *allocation_name, const T &ot
     } \
     void *operator new[](size_t num_bytes) \
     { \
-      return MEM_mallocN(num_bytes, _id "[]"); \
+      return MEM_mallocN_aligned(num_bytes, __STDCPP_DEFAULT_NEW_ALIGNMENT__, _id "[]"); \
     } \
     void *operator new[](size_t num_bytes, std::align_val_t alignment) \
     { \
-- 
GitLab