Wayland touch: a finger's slot keeps a destroyed window unless it was the touch window, so its next motion or up uses freed memory #191

Open
opened 2026-11-17 06:45:34 +00:00 by jorijnvdgraaf · 0 comments

Summary

Every finger that is down has a TouchSlot holding the window it went down on, and touch_up and touch_motion take the window from the slot, since those events carry no surface. ~Window forgets the slots only when the window being destroyed is Device::touchWindow, the window of the last finger down. With fingers on two windows, destroying the other one leaves its fingers' slots pointing at it, and the next wl_touch.motion or wl_touch.up for such a finger writes to the freed window and fires its events. Line numbers refer to c006519.

Details

  • implementations/Crafter.Graphics-Device.cpp:725 and :732: touch_down sets touchWindow = window and slot->window = window.
  • implementations/Crafter.Graphics-Window.cpp:695-698: ~Window runs Device::ForgetTouches() only if (Device::touchWindow == this).
  • Device.cpp:754 (touch_up) and :774 (touch_motion): window = slot->window, then window->lastInputTime = time, window->onTouchUp.Invoke(...) or onTouchMove.Invoke(...), and the reads of window->primaryTouchId after them.

Concrete case

Finger 7 goes down on window A: slot 7 holds A, and touchWindow is A. Finger 8 goes down on window B: touchWindow is B. A is destroyed. touchWindow is B, so slot 7 keeps A. Finger 7 moves or lifts: touchWindow is not null, SlotFor(7) finds the slot, and the handler uses A.

A slot whose up never comes also stays used, so AnyTouch() stays true and touch_up no longer clears touchWindow (:767) until a later finger goes down with the same id.

Found by reading the code, in the pre-push review of 364d210..c006519. It is older than that range, which added one write through the same pointer. Not reproduced, and whether a compositor still sends motion or up for a touch point whose surface is gone was not checked against one.

Suggested fix

In ~Window, clear every slot whose window is this, whichever window touchWindow is, and clear touchWindow when it is this or no slot is left in use.

## Summary Every finger that is down has a `TouchSlot` holding the window it went down on, and `touch_up` and `touch_motion` take the window from the slot, since those events carry no surface. `~Window` forgets the slots only when the window being destroyed is `Device::touchWindow`, the window of the last finger down. With fingers on two windows, destroying the other one leaves its fingers' slots pointing at it, and the next `wl_touch.motion` or `wl_touch.up` for such a finger writes to the freed window and fires its events. Line numbers refer to `c006519`. ## Details - `implementations/Crafter.Graphics-Device.cpp:725` and `:732`: `touch_down` sets `touchWindow = window` and `slot->window = window`. - `implementations/Crafter.Graphics-Window.cpp:695-698`: `~Window` runs `Device::ForgetTouches()` only `if (Device::touchWindow == this)`. - `Device.cpp:754` (`touch_up`) and `:774` (`touch_motion`): `window = slot->window`, then `window->lastInputTime = time`, `window->onTouchUp.Invoke(...)` or `onTouchMove.Invoke(...)`, and the reads of `window->primaryTouchId` after them. ## Concrete case Finger 7 goes down on window A: slot 7 holds A, and `touchWindow` is A. Finger 8 goes down on window B: `touchWindow` is B. A is destroyed. `touchWindow` is B, so slot 7 keeps A. Finger 7 moves or lifts: `touchWindow` is not null, `SlotFor(7)` finds the slot, and the handler uses A. A slot whose up never comes also stays `used`, so `AnyTouch()` stays true and `touch_up` no longer clears `touchWindow` (`:767`) until a later finger goes down with the same id. Found by reading the code, in the pre-push review of 364d210..c006519. It is older than that range, which added one write through the same pointer. Not reproduced, and whether a compositor still sends motion or up for a touch point whose surface is gone was not checked against one. ## Suggested fix In `~Window`, clear every slot whose `window` is `this`, whichever window `touchWindow` is, and clear `touchWindow` when it is `this` or no slot is left in use.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Catcrafts/Crafter.Graphics#191
No description provided.