From 50023eab75dd0632ae508e3d0fd007ee7fb83849 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Tue, 22 Apr 2025 14:32:39 +0200 Subject: [PATCH] Fix: Invalid kdtree node allocation after recent refactor Address the root cause and fix one definition rule violation. Broken by fb2ba20b67. Pull Request: https://projects.blender.org/blender/blender/pulls/137845 --- source/blender/blenlib/intern/kdtree_impl.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/source/blender/blenlib/intern/kdtree_impl.h b/source/blender/blenlib/intern/kdtree_impl.h index 3bab78ec3c6..0260e6fc41d 100644 --- a/source/blender/blenlib/intern/kdtree_impl.h +++ b/source/blender/blenlib/intern/kdtree_impl.h @@ -22,6 +22,11 @@ #define _BLI_KDTREE_CONCAT(MACRO_ARG1, MACRO_ARG2) _BLI_KDTREE_CONCAT_AUX(MACRO_ARG1, MACRO_ARG2) #define BLI_kdtree_nd_(id) _BLI_KDTREE_CONCAT(KDTREE_PREFIX_ID, _##id) +/* Put in anonymouse namespace to avoid violating one definition rule. + * Otherwise MEM_malloc_array can get defined once for multiple dimensions, + * with different node sizes. */ +namespace { + struct KDTreeNode_head { uint left, right; float co[KD_DIMS]; @@ -35,6 +40,8 @@ struct KDTreeNode { uint d; /* range is only (0..KD_DIMS - 1) */ }; +} // namespace + struct KDTree { KDTreeNode *nodes; uint nodes_len; @@ -95,10 +102,6 @@ KDTree *BLI_kdtree_nd_(new)(uint nodes_len_capacity) KDTree *tree; tree = MEM_callocN("KDTree"); - /* NOTE: Cannot use `MEM_malloc_arrayN()` here, as `KDTreeNode` is not one type, but - * four (1D to 4D), differing by their `float co[KD_DIMS]` member. It seems like the code - * generating the templates does not distinguish these cases, and create a single code for all - * four cases, leading to invalid allocation sizes. */ tree->nodes = MEM_malloc_arrayN(nodes_len_capacity, "KDTreeNode"); tree->nodes_len = 0; tree->root = KD_NODE_ROOT_IS_INIT;