New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling godot::String::ascii() or utf8() causes memory corruption if the string originated from GDScript. #40957
Comments
It's probably GDNative C++ wrapper bug With #40999, there are tests for |
Can confirm this is still an issue in 3.3.3 stable. A single call to While digging I found that A workaround if you need #include <GodotGlobal.hpp>
...
godot::String my_string;
...
const auto utf8_string = my_string.alloc_c_string();
// Do something with utf8_string
godot::api->godot_free(utf8_string); |
Is this still an issue in 3.5? |
Probably yes, since the godot-cpp String implementation has not changed in any meaningful way since early 2021. |
To be honest godot-cpp's
class CharString {
friend class String;
godot_char_string _char_string;
public:
~CharString();
int length() const;
const char *get_data() const;
};
...
class String {
...
CharString utf8() const;
CharString ascii(bool p_extended = false) const;
...
};
godot::CharString::~CharString() {
godot::api->godot_char_string_destroy(&_char_string);
}
int godot::CharString::length() const {
return godot::api->godot_char_string_length(&_char_string);
}
const char *godot::CharString::get_data() const {
return godot::api->godot_char_string_get_data(&_char_string);
} As you can see |
I'm honestly a little shocked that so far the developers haven't considered this a showstopper. If you ask me, "If you call this public API function, your code will crash" should stop any release dead in its tracks. |
+1 on this being extremely important to fix.
If anyone else is dealing with this in iOS plugins, where the |
hey do you maybe know how to pull this off in godot 4 gdextension? |
Godot version:
3.2-stable. Also tried with 3.2 built locally (23b553b)
OS/device including version:
Windows 10
Issue description:
Passing a String into a GDNative c++ plugin and then simply calling
ascii()
on it and doing nothing else seems to cause some kind of memory corruption (WinDbg will catch it https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/debugger-download-tools).Steps to reproduce:
gdscript
c++
utf8()
has the same problem. Also happens if the string belongs to a godot::Dictionary. If the string was instantiated from GDNative then these functions seem fine.The fun of this is that godot will probably crash somewhere completely unrelated, or might not crash at all. Running through WinDbg seems to catch the problem early but the stack trace it gives you will vary depending on what your project is doing and which build of Godot you use.
Example stack trace from WinDbg:
The few clues I picked up during my hair-pulling hell day lead me to think the problem lies somewhere in the
CharString
destructor but I've now found a workaround so I officially don't care.Workarounds:
to_ascii()
in GDScript and pass in aPoolByteArray
instead. Then convert it back to a string in GDNative.std::string to_ascii(String s)
The text was updated successfully, but these errors were encountered: