From f456e9cdc895ee03b0c563cded08ea60af6b8164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bas=20Magr=C3=A9?= Date: Tue, 3 Nov 2026 18:33:52 +0100 Subject: [PATCH] togl: fix crash on startup under Wayland When an extension is not listed in GL_EXTENSIONS, CheckOpenGLExtension falls back to glXQueryExtensionsString(glXGetCurrentDisplay()). Under Wayland SDL uses EGL: there is no GLX context and the GLX entry points returned via SDL_GL_GetProcAddress are unusable stubs, so the result was passed to strstr() and crashed. Only query GLX extensions when SDL's current video driver is x11 (looked up with dlsym, since togl does not link SDL), and skip NULL results. Co-Authored-By: Claude --- togl/linuxwin/glentrypoints.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/togl/linuxwin/glentrypoints.cpp b/togl/linuxwin/glentrypoints.cpp index 621ab394ff..0a934875dc 100644 --- a/togl/linuxwin/glentrypoints.cpp +++ b/togl/linuxwin/glentrypoints.cpp @@ -45,6 +45,7 @@ #if (defined(_LINUX) || defined(PLATFORM_BSD)) && !defined(__ANDROID__) #include +#include #endif // NOTE: This has to be the last file included! @@ -301,9 +302,22 @@ static bool CheckOpenGLExtension_internal(const char *ext, const int coremajor, { static CDynamicFunctionOpenGL< true, Display *( APIENTRY *)( ), Display* > glXGetCurrentDisplay("glXGetCurrentDisplay"); static CDynamicFunctionOpenGL< true, const char *( APIENTRY *)( Display*, int ), const char * > glXQueryExtensionsString("glXQueryExtensionsString"); - if (glXQueryExtensionsString && glXGetCurrentDisplay) + // Under Wayland, SDL uses EGL: there is no GLX context, and the GLX entry + // points returned by SDL_GL_GetProcAddress/eglGetProcAddress are unusable + // stubs. Only query GLX extensions when SDL's video driver is x11. + static int s_bIsX11 = -1; + if (s_bIsX11 < 0) { - extensions = glXQueryExtensionsString(glXGetCurrentDisplay(), 0); + typedef const char *(*SDL_GetCurrentVideoDriver_t)(void); + SDL_GetCurrentVideoDriver_t pfn = (SDL_GetCurrentVideoDriver_t)dlsym(RTLD_DEFAULT, "SDL_GetCurrentVideoDriver"); + const char *drv = pfn ? pfn() : NULL; + s_bIsX11 = (!drv || !strcmp(drv, "x11")) ? 1 : 0; + } + Display *dpy = (s_bIsX11 && glXQueryExtensionsString && glXGetCurrentDisplay) ? glXGetCurrentDisplay() : NULL; + const char *glxext = dpy ? glXQueryExtensionsString(dpy, 0) : NULL; + if (glxext) + { + extensions = glxext; ptr = strstr(extensions, ext); } }