Skip to content

Possible heap corruption issue #842

Description

@lightyears1998

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis has been identified as a bugtopic:gdextensionThis relates to the new Godot 4 extension implementation

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions