SpaiR / imgui-java

JNI based binding for Dear ImGui
MIT License
547 stars 90 forks source link

Bug: ImGuiInputTextCallbackData#insertChars handles UTF-8 characters incorrectly #207

Open phoenix-the-uncreative opened 8 months ago

phoenix-the-uncreative commented 8 months ago

Version

1.86.11

What happened?

ImGuiInputTextCallbackData#insertChars works fine when inserting ASCII characters, but inserts wrong characters when trying to add anything else.

Reproduction

When setting the buffer returned by an InputText callback like so:

String newText = "new text";
data.setBufDirty(true);
data.deleteChars(0, data.getBuf().length());
data.insertChars(0, newText);

It works perfectly when using only ASCII characters. However, if you use UTF-8 characters (for example, Cyrillic), the buffer is also fed with some incorrect bytes.

For example, here's how the buffer looks before writing in Russian:

"hello"

And here's how it looks after typing "я":

"helloя�"

If you continue typing, the application will crash entirely with an assertion failure.

Relevant log output

[04:04:40] [Render thread/INFO] [STDERR/]: [imgui.ImGui$1:imAssertCallback:92]: Dear ImGui Assertion Failed: pos + bytes_count <= BufTextLen
[04:04:40] [Render thread/INFO] [STDERR/]: [imgui.ImGui$1:imAssertCallback:93]: Assertion Located At: /tmp/imgui/jni/imgui_widgets.cpp:3820
[04:04:40] [Render thread/INFO] [STDERR/]: [java.lang.Thread:dumpStack:1383]: java.lang.Exception: Stack trace
[04:04:40] [Render thread/INFO] [STDERR/]: [java.lang.Thread:dumpStack:1383]:   at java.base/java.lang.Thread.dumpStack(Thread.java:1383)
[04:04:40] [Render thread/INFO] [STDERR/]: [java.lang.Thread:dumpStack:1383]:   at imgui.ImGui$1.imAssertCallback(ImGui.java:94)
[04:04:40] [Render thread/INFO] [STDERR/]: [java.lang.Thread:dumpStack:1383]:   at imgui.assertion.ImAssertCallback.imAssert(ImAssertCallback.java:21)
[04:04:40] [Render thread/INFO] [STDERR/]: [java.lang.Thread:dumpStack:1383]:   at imgui.ImGuiInputTextCallbackData.deleteChars(Native Method)