Skip to content

active_input: guard against unsigned timestamp underflow in debounce check - #7219

Open
ilyasssif wants to merge 1 commit into
ItsNotRexed:mainfrom
ilyasssif:fix/active-input-timestamp-underflow
Open

ilyasssif wants to merge 1 commit into
ItsNotRexed:mainfrom
ilyasssif:fix/active-input-timestamp-underflow

Conversation

@ilyasssif

Copy link
Copy Markdown

Problem

In runtime/src/active_input.h (ActiveInputTracker::observe), the debounce check computes time_us - changed_at_ < thresholds_.min_switch_us on std::uint64_t timestamps. If an out-of-order sample arrives with time_us < changed_at_, the unsigned subtraction wraps around to a huge value (~UINT64_MAX), bypassing min_switch_us and overwriting changed_at_ backwards.

Fix

  • Guard time_us < changed_at_ before subtracting in ActiveInputTracker::observe() so stale/out-of-order timestamps are ignored instead of underflowing.
  • Added a regression check in runtime/test/active_input_test.cpp (test_min_switch_time).

Testing

Synthetic inputs only; no game files and no GPU used. Tested on Fedora Linux 43:

  • Regression test on main (43a9c04): FAIL (line 118: t.observe(xbox, InputFamily::Xbox, button(1), 1850 * ms) == InputEdge::None)
  • g++ -std=c++20 -Wall -Wextra -Werror runtime/test/active_input_test.cpp: 7/7 pass
  • clang++ -std=c++20 -Wall -Wextra -Werror runtime/test/active_input_test.cpp: 7/7 pass

AI-assisted: yes.

@AirKyzzZ

Copy link
Copy Markdown
Contributor

Tested d2f99ad16 on RTX 2060 (NVIDIA 566.14), Windows 10 22H2, clang-cl 23.1.6 + lld-link (MSVC 14.44 STL), SDK 10.0.26100: pass

Built active_input_test at the head as is. ctest -R "^active_input$": 1/1 pass, 7 of 7 cases. With active_input.h from the merge base 43a9c0450: 6 of 7, line 118: t.observe(xbox, InputFamily::Xbox, button(1), 1850 * ms) == InputEdge::None fails as you describe, so the new check fails on main. CPU only, no game files. AI-assisted (Claude Code).

@philip-roy-jones

Copy link
Copy Markdown

I tested whether an out-of-order input could move the 300 ms controller-switch deadline. I used the public ActiveInputTracker API with synthetic button presses on Windows 11 x64 (MSVC 19.51), at d2f99ad16 and its parent 43a9c045.

Both builds first switched to PlayStation 5 at 1,000,000 microseconds. I then sent these three presses in this order.

Timestamp (microseconds) Input PR Parent
999,999 Xbox Stayed on PlayStation 5 Switched to Xbox
1,299,999 Nintendo Stayed on PlayStation 5 Switched to Nintendo
1,300,000 Xbox, one additional button pressed Switched to Xbox Stayed on Nintendo

The fresh Xbox press at exactly 300 ms was accepted on the PR. The same probe matched all 7 expected observations on the PR and 4 of 7 on the parent.

AI-assisted verification.

@senyurah

Copy link
Copy Markdown

d2f99ad16 passed all 7 active_input_test cases on Windows with MSYS2 UCRT64 GCC 15.2.0. One synthetic edge case remains if “stale samples are ignored” includes the per-device baseline.

  1. Baseline the PS5 pad at 999,998 µs. At 1,000,000 µs baseline Xbox with button 1 held, then switch to PS5 with a PS5 press.
  2. Insert an Xbox release stamped 999,999 µs.
  3. At 1,300,000 µs send another Xbox sample with button 1 still held.

With step 2, the final sample returns Button and switches to Xbox. Without step 2, it returns None. The stale release overwrites it->second before the guard, so a held button can look like a new press after the window. If the scope is only preventing the stale sample itself from switching families, the current fix does that. If stale samples should have no effect, check the timestamp before updating the baseline and add this case. I tested the tracker API with synthetic inputs only. AI-assisted (Codex).

@myokex

myokex commented Nov 23, 2026

Copy link
Copy Markdown

Tested d2f99ad16 merged onto main 042c5f378 on Windows 11, Visual Studio 2022 (MSVC, RelWithDebInfo), CMake 4.4.4, Vulkan SDK 1.4.363.0, AMD Radeon RX 7800 XT (driver 32.0.32029.2010); synthetic tests only, no game files: pass.

Not checked: that the new case fails without the time_us < changed_at_ guard (I did not rebuild the test without it). AI-assisted (Claude Code).

@karearl

karearl commented Nov 23, 2026

Copy link
Copy Markdown

Baseline on main here: d3d12_vulkan_device, d3d12_present and xbox_present fail on this machine without the PR too; no Vulkan SDK installed, so the glslang/llvm-mc shader tests were not built.

AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants