From 6d10b55720414a5480472ddff431ba01f12402fc Mon Sep 17 00:00:00 2001 From: LemonHeadOnGit <41242131+LemonHeadOnGit@users.noreply.github.com> Date: Mon, 4 Aug 2025 02:59:19 +0200 Subject: [PATCH] Fixed a case of a null value causing a segfault window.GetKeyName could return a null value causing a segmentation fault under certain circumstances. This has been mitigated by handling the GetKeyName function outside of the ImGui block. --- src/UserInterface.cpp | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/UserInterface.cpp b/src/UserInterface.cpp index 787c5585..4a97fb36 100644 --- a/src/UserInterface.cpp +++ b/src/UserInterface.cpp @@ -176,16 +176,34 @@ void UserInterface::DrawSettings() const auto& window = descriptorPool_->Device().Surface().Instance().Window(); + constexpr int GLFW_KEY_CODES[] = { + GLFW_KEY_W, + GLFW_KEY_A, + GLFW_KEY_S, + GLFW_KEY_D + }; + + const char FALLBACK_KEY_CHAR[4] = {'W', 'A', 'S', 'D'}; + char glfw_key_display[4]; + + { + for (int i = 0; i < 4; ++i) { + auto name = window.GetKeyName(GLFW_KEY_CODES[i], 0); + char c = name ? name[0] : char(FALLBACK_KEY_CHAR[i]); // fallback 'W','A','S','D' + glfw_key_display[i] = std::toupper(static_cast(c)); + } + } + ImGui::Text("Help"); ImGui::Separator(); ImGui::BulletText("F1: toggle Settings."); ImGui::BulletText("F2: toggle Statistics."); ImGui::BulletText( "%c%c%c%c/SHIFT/CTRL: move camera.", - std::toupper(window.GetKeyName(GLFW_KEY_W, 0)[0]), - std::toupper(window.GetKeyName(GLFW_KEY_A, 0)[0]), - std::toupper(window.GetKeyName(GLFW_KEY_S, 0)[0]), - std::toupper(window.GetKeyName(GLFW_KEY_D, 0)[0])); + glfw_key_display[0], + glfw_key_display[1], + glfw_key_display[2], + glfw_key_display[3]); ImGui::BulletText("L/R Mouse: rotate camera/scene."); ImGui::NewLine();