Stack buffer overrun for enum function arguments in gdextension API, affecting at least godot-cpp #101639
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I ran into a stack buffer overrun, code reading extra memory that it should not, only to throw these extra bytes away.
This bug was detected with address sanitizer (GCC -fsanitize=address), and prevents anyone from using address sanitizer in combination with godot-cpp, because address sanitizer aborts on first error. Otherwise the impact of this bug is probably low.
However, allowing use of address sanitizer by contributors does seem like it will benefit the quality of the godot codebase long term. There is no realistic workaround if one wishes to use address sanitizer with godot-cpp, because any call to the godot API that involves an enum parameter will trigger the buffer overrun + abort by asan.
Bug details: the affected code receives a pointer to an enum as a void pointer, and reinterpret casts it to an int64 pointer, probably because int64 is the size used for variants. But enums are not 64 bit by default, and the majority of enums in the godot codebase use the default size, which is usually int (32 bit on most platforms), and never larger unless absolutely needed, see C++ standard on enum size
Since the code immediately does a truncating C-style cast of this int64 to the enum type, the 4 extra bytes will be discarded immediately and not result in any program logic errors.