From f039ea68d0e27973fe1996cd2e20b056f41026d3 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 6 Oct 2022 09:35:18 +0200 Subject: [PATCH] fix: Potential crashes when pasting with no clipboard content --- lib/external/imgui/source/imgui_impl_glfw.cpp | 11 +++++++---- .../builtin/source/content/views/view_hex_editor.cpp | 2 ++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/external/imgui/source/imgui_impl_glfw.cpp b/lib/external/imgui/source/imgui_impl_glfw.cpp index ac025c75f..b1c863ac0 100644 --- a/lib/external/imgui/source/imgui_impl_glfw.cpp +++ b/lib/external/imgui/source/imgui_impl_glfw.cpp @@ -164,7 +164,10 @@ static void ImGui_ImplGlfw_ShutdownPlatformInterface(); // Functions static const char* ImGui_ImplGlfw_GetClipboardText(void* user_data) { - return glfwGetClipboardString((GLFWwindow*)user_data); + // IMHEX PATCH BEGIN + const char *data = glfwGetClipboardString((GLFWwindow*)user_data); + return data == nullptr ? "" : data; + // IMHEX PATCH END } static void ImGui_ImplGlfw_SetClipboardText(void* user_data, const char* text) @@ -179,12 +182,12 @@ static void ImGui_ImplGlfw_SetClipboardText(void* user_data, const char* text) ImGuiContext& g = *GImGui; g.ClipboardHandlerData.clear(); if (!::OpenClipboard(NULL)) - return NULL; + return ""; HANDLE wbuf_handle = ::GetClipboardData(CF_UNICODETEXT); if (wbuf_handle == NULL) { ::CloseClipboard(); - return NULL; + return ""; } if (const WCHAR* wbuf_global = (const WCHAR*)::GlobalLock(wbuf_handle)) { @@ -194,7 +197,7 @@ static void ImGui_ImplGlfw_SetClipboardText(void* user_data, const char* text) } ::GlobalUnlock(wbuf_handle); ::CloseClipboard(); - return g.ClipboardHandlerData.Data; + return g.ClipboardHandlerData.Data == nullptr ? "" : g.ClipboardHandlerData.Data; } static void ImGui_ImplWin_SetClipboardText(void*, const char* text) diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index e230b20c3..9f798197d 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -1222,6 +1222,8 @@ namespace hex::plugin::builtin { auto provider = ImHexApi::Provider::get(); std::string clipboard = ImGui::GetClipboardText(); + if (clipboard.empty()) + return; // Check for non-hex characters bool isValidHexString = std::find_if(clipboard.begin(), clipboard.end(), [](char c) {