From e1bb69b3b5a7d616f68321313e1264dbbc49fb1d Mon Sep 17 00:00:00 2001 From: James Cowgill Date: Wed, 4 Dec 2024 23:13:01 +0000 Subject: [PATCH] Fix segfault if XOpenIM fails If `XOpenIM` fails we will store a `nullptr` into `sharedXIM`. When the shared `XIM` is destroyed we call the deleter `XCloseIM` on the `nullptr` which segfaults. Fix this by adding a new `closeIM` helper function which checks for null first. --- src/SFML/Window/Unix/Display.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/SFML/Window/Unix/Display.cpp b/src/SFML/Window/Unix/Display.cpp index 918382652..356077822 100644 --- a/src/SFML/Window/Unix/Display.cpp +++ b/src/SFML/Window/Unix/Display.cpp @@ -104,7 +104,12 @@ std::shared_ptr<_XIM> openXim() XSetLocaleModifiers(""); // Create the input context - sharedXIM.reset(XOpenIM(UnixDisplayImpl::weakSharedDisplay.lock().get(), nullptr, nullptr, nullptr), XCloseIM); + const auto closeIM = [](XIM im) + { + if (im) + XCloseIM(im); + }; + sharedXIM.reset(XOpenIM(UnixDisplayImpl::weakSharedDisplay.lock().get(), nullptr, nullptr, nullptr), closeIM); xim = sharedXIM; // Restore the previous locale