If my understanding is correct, the current implementation of memnew_arr_template and memdelete_arr could lead to heap corruption.
|
size_t len = sizeof(T) * p_elements; |
|
uint64_t *mem = (uint64_t *)Memory::alloc_static(len); |
|
T *failptr = nullptr; // Get rid of a warning. |
|
ERR_FAIL_COND_V(!mem, failptr); |
|
*(mem - 1) = p_elements; |
Line 146 *(mem - 1) = p_elements; assumes that godot-cpp version of Memory::alloc_static has allocated extra bytes before actual array, which is not true. Memory::alloc_static internally calls mem_alloc from gd extension interface. mem_alloc function is implemented in godot/core/extension/gdnative_interface.cpp as a call to a macro memalloc(p_size), which expands to the godot upstream version of Memory::alloc_static(m_size), and godot upstream version of Memory::alloc_static doesn't pre-allocate extras bytes before the actual array.
Suggested fix: master...lightyears1998:godot-cpp:fix-heap-corruption
If my understanding is correct, the current implementation of
memnew_arr_templateandmemdelete_arrcould lead to heap corruption.godot-cpp/include/godot_cpp/core/memory.hpp
Lines 142 to 146 in be7ed4c
Line 146
*(mem - 1) = p_elements;assumes that godot-cpp version ofMemory::alloc_statichas allocated extra bytes before actual array, which is not true.Memory::alloc_staticinternally callsmem_allocfrom gd extension interface.mem_allocfunction is implemented in godot/core/extension/gdnative_interface.cpp as a call to a macromemalloc(p_size), which expands to the godot upstream version ofMemory::alloc_static(m_size), and godot upstream version ofMemory::alloc_staticdoesn't pre-allocate extras bytes before the actual array.Suggested fix: master...lightyears1998:godot-cpp:fix-heap-corruption