From a0c5d0a96b99fd43dff0f3f68efb60681175639f Mon Sep 17 00:00:00 2001 From: Keavon Chambers Date: Fri, 18 Sep 2026 22:48:56 -0700 Subject: [PATCH] win32: fix crash when moving a window that owns non-winit windows (#4700) This fixes a crash introduced by #4646. It has already shipped in 0.31.0-beta.3, so hopefully this fix can be merged before the next stable release. We hit it by dragging the window in our project, [Graphite](https://github.com/GraphiteEditor/Graphite), which freezes the app on the first move and followed shortly by a crash to desktop. `reposition_owned_windows` runs on every `WM_WINDOWPOSCHANGED` that moves a window. It enumerates all windows on the thread that are owned by the moved window, reads each one's `GWL_USERDATA`, and dereferences the value as `*mut WindowData`: ```rs let userdata_ptr = unsafe { util::get_window_long(hwnd, GWL_USERDATA) } as *mut WindowData; if !userdata_ptr.is_null() { let userdata = unsafe { &*userdata_ptr }; ``` Nothing checks that the enumerated window was created by winit. `GWL_USERDATA` is free for any window's creator to use, so any owned window that stores something non-zero there is dereferenced as a winit struct. That covers windows the application creates itself through `raw-window-handle`, and potentially dialogs or controls created on the same thread by other libraries. In Graphite, we are drawing a custom frame and implementing native resize borders with an invisible helper window owned by the winit window. The helper stores the main window's `HWND` in `GWLP_USERDATA`. Moving the winit window therefore crashes on the first position change, every time: - `0xC0000005` access violation in `winit_win32::event_loop::reposition_owned_windows::enum_proc` - followed by `0xC000041D` (`STATUS_FATAL_USER_CALLBACK_EXCEPTION`), since the fault happens inside the window procedure while Windows is dispatching the message This PR fixes it by comparing the window's `GWL_WNDPROC` against `public_window_callback` before touching `GWL_USERDATA` and skipping the window when they differ. Winit's window procedure is what defines the meaning of that slot, so this is the narrowest test that the pointer really is a `WindowData`. Anchored popups still follow their owner with this change. To reproduce outside of Graphite, on winit's `master`, in any of its examples: create a top-level Win32 window (e.g. `WS_POPUP`) with `CreateWindowExW`, passing the winit window's `HWND` as the owner, call `SetWindowLongPtrW(helper, GWLP_USERDATA, 1)`, then move the winit window by dragging it or calling `set_outer_position`. It dies on the first move. A debug build panics with "misaligned pointer dereference" at the `&*userdata_ptr` line, and a release build exits with `0xC000041D`. With this PR it does not. Tested on Windows 11 25H2 (build 26200.9445). --- winit-win32/src/event_loop.rs | 9 +++++++++ winit/src/changelog/unreleased.md | 1 + 2 files changed, 10 insertions(+) diff --git a/winit-win32/src/event_loop.rs b/winit-win32/src/event_loop.rs index ca29be1b..a4be8cca 100644 --- a/winit-win32/src/event_loop.rs +++ b/winit-win32/src/event_loop.rs @@ -1118,10 +1118,19 @@ unsafe fn lose_active_focus(window: HWND, userdata: &WindowData) { /// has to be done manually whenever `parent` receives a `WM_WINDOWPOSCHANGED` that moved it. unsafe fn reposition_owned_windows(parent: HWND) { unsafe extern "system" fn enum_proc(hwnd: HWND, lparam: LPARAM) -> BOOL { + use windows_sys::Win32::UI::WindowsAndMessaging::GWL_WNDPROC; + if unsafe { GetWindow(hwnd, GW_OWNER) } != lparam as HWND { return true.into(); // continue enumeration } + // Only winit's own windows keep a `WindowData` pointer in `GWL_USERDATA`, so skip any + // owned window that runs a different window procedure (dialogs, tooltips, helpers) + let window_proc = unsafe { util::get_window_long(hwnd, GWL_WNDPROC) }; + if window_proc != public_window_callback as *const () as isize { + return true.into(); // continue enumeration + } + let userdata_ptr = unsafe { util::get_window_long(hwnd, GWL_USERDATA) } as *mut WindowData; if !userdata_ptr.is_null() { let userdata = unsafe { &*userdata_ptr }; diff --git a/winit/src/changelog/unreleased.md b/winit/src/changelog/unreleased.md index 2a6b3728..a0e7d3a4 100644 --- a/winit/src/changelog/unreleased.md +++ b/winit/src/changelog/unreleased.md @@ -44,6 +44,7 @@ changelog entry. - On Windows, fix a crash occurring when trying to create a DXGI surface on a window that is both fullscreen and always on top. - On Wayland, use the touch serial for `Window::drag_window`, `Window::drag_resize_window` and `Window::show_window_menu` when the interaction was started by touch. +- On Windows, fix a crash when moving a window that owns windows not created by winit, such as native dialogs or application helper windows. ### Changed