From 7011d87bf80f80f5bc9a43c5bb07eef577798c26 Mon Sep 17 00:00:00 2001 From: omar Date: Wed, 22 Aug 2018 21:26:38 +0200 Subject: [PATCH] InputText: Improved sturdiness if the ResizeCallback purposefully modify data->BufTextLen or data->BufSize. (#2006, #1443, #1008) --- imgui.cpp | 9 ++++++--- misc/stl/imgui_stl.cpp | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index c1fff701f..ba7bf994b 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -11163,15 +11163,18 @@ bool ImGui::InputTextEx(const char* label, char* buf, int buf_size, const ImVec2 callback_data.EventFlag = ImGuiInputTextFlags_CallbackResize; callback_data.Flags = flags; callback_data.Buf = buf; - callback_data.BufTextLen = edit_state.CurLenA; + callback_data.BufTextLen = apply_new_text_length; callback_data.BufSize = ImMax(buf_size, apply_new_text_length + 1); callback_data.UserData = callback_user_data; callback(&callback_data); buf = callback_data.Buf; buf_size = callback_data.BufSize; + apply_new_text_length = ImMin(callback_data.BufTextLen, buf_size - 1); + IM_ASSERT(apply_new_text_length <= buf_size); } - IM_ASSERT(apply_new_text_length <= buf_size); - ImStrncpy(buf, edit_state.TempBuffer.Data, apply_new_text_length + 1); + + // If the underlying buffer resize was denied or not carried to the next frame, apply_new_text_length+1 may be >= buf_size. + ImStrncpy(buf, edit_state.TempBuffer.Data, ImMin(apply_new_text_length + 1, buf_size)); value_changed = true; } diff --git a/misc/stl/imgui_stl.cpp b/misc/stl/imgui_stl.cpp index c504e98ae..d3d5e35d6 100644 --- a/misc/stl/imgui_stl.cpp +++ b/misc/stl/imgui_stl.cpp @@ -34,6 +34,7 @@ static int InputTextCallback(ImGuiInputTextCallbackData* data) bool ImGui::InputText(const char* label, std::string* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) { + IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0); flags |= ImGuiInputTextFlags_CallbackResize; InputTextCallback_UserData cb_user_data; @@ -45,6 +46,7 @@ bool ImGui::InputText(const char* label, std::string* str, ImGuiInputTextFlags f bool ImGui::InputTextMultiline(const char* label, std::string* str, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) { + IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0); flags |= ImGuiInputTextFlags_CallbackResize; InputTextCallback_UserData cb_user_data;