#2899·glfw

Wayland: `glfwGetKeyName` segfaults when xkb state is not yet initialized

Author: sephirokCreated Sep 13, 2026Updated Sep 13, 2026

glfwGetKeyName is documented as valid after glfwInit (no window required) and is supposed to return NULL when a layout name is unavailable. On Wayland it instead segfaults if the compositor has not yet sent a keymap.

_glfwGetScancodeNameWayland calls xkb_state_key_get_layout(_glfw.wl.xkb.state, keycode) with no NULL check. _glfw.wl.xkb.state is only set in keyboardHandleKeymap, which often does not run until after the first window exists. The X11 equivalent already bails out:

c
const char* _glfwGetScancodeNameX11(int scancode)
{
    if (!_glfw.x11.xkb.available)
        return NULL;

OS: Linux (Wayland, e.g. GNOME/Mutter)
GLFW: 3.5.1 (also current master src/wl_window.c)
Repro: glfwInit() then glfwGetKeyName(GLFW_KEY_A, 0) before glfwCreateWindow.

c
#include <GLFW/glfw3.h>
#include <stdio.h>

int main(void)
{
    if (!glfwInit())
        return 1;
    const char* name = glfwGetKeyName(GLFW_KEY_A, 0);
    printf("%s\n", name ? name : "(null)");
    glfwTerminate();
    return 0;
}

Build with a Wayland-enabled GLFW (GLFW_BUILD_WAYLAND=ON) and run on a Wayland session (XDG_SESSION_TYPE=wayland).

Actual: SIGSEGV in xkb_state_key_get_layout (SEGV_MAPERR, address 0x80).
Expected: return NULL (optionally GLFW_PLATFORM_ERROR), matching X11 and the glfwGetKeyName docs.

Suggested fix in _glfwGetScancodeNameWayland:

c
if (!_glfw.wl.xkb.state)
    return NULL;