From 26920a1a38d7c4c5b7908cf2e04b750d941f1faa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Wed, 25 May 2022 19:43:55 +0200 Subject: [PATCH] X11: Fix previous error handler not being restored GLFW did not restore the previous Xlib error handler when removing its own, instead resetting to the default handler. This commit saves and restores the previous error handler. None of this is thread-safe or could ever be. Fixes #2108 --- README.md | 1 + src/x11_init.c | 7 +++++-- src/x11_platform.h | 2 ++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f189ba20..71ee73f0 100644 --- a/README.md +++ b/README.md @@ -292,6 +292,7 @@ information on what to include when reporting a bug. - [X11] Bugfix: Left shift of int constant relied on undefined behavior (#1951) - [X11] Bugfix: The OSMesa libray was not unloaded on termination - [X11] Bugfix: A malformed response during selection transfer could cause a segfault + - [X11] Bugfix: Some calls would reset Xlib to the default error handler (#2108) - [Wayland] Added dynamic loading of all Wayland libraries - [Wayland] Added support for key names via xkbcommon - [Wayland] Added support for file path drop events (#2040) diff --git a/src/x11_init.c b/src/x11_init.c index aefa82db..11aeb9e5 100644 --- a/src/x11_init.c +++ b/src/x11_init.c @@ -37,6 +37,7 @@ #include #include #include +#include // Translate the X11 KeySyms for a key to a GLFW key code @@ -1095,8 +1096,9 @@ static int errorHandler(Display *display, XErrorEvent* event) // void _glfwGrabErrorHandlerX11(void) { + assert(_glfw.x11.errorHandler == NULL); _glfw.x11.errorCode = Success; - XSetErrorHandler(errorHandler); + _glfw.x11.errorHandler = XSetErrorHandler(errorHandler); } // Clears the X error handler callback @@ -1105,7 +1107,8 @@ void _glfwReleaseErrorHandlerX11(void) { // Synchronize to make sure all commands are processed XSync(_glfw.x11.display, False); - XSetErrorHandler(NULL); + XSetErrorHandler(_glfw.x11.errorHandler); + _glfw.x11.errorHandler = NULL; } // Reports the specified error, appending information about the last X error diff --git a/src/x11_platform.h b/src/x11_platform.h index ff0676dd..ecaa0fa4 100644 --- a/src/x11_platform.h +++ b/src/x11_platform.h @@ -567,6 +567,8 @@ typedef struct _GLFWlibraryX11 XContext context; // XIM input method XIM im; + // The previous X error handler, to be restored later + XErrorHandler errorHandler; // Most recent error code received by X error handler int errorCode; // Primary selection string (while the primary selection is owned)