runtime: guard window-fit zero dimensions, signed overflow, and OOM in lifecycle shim #2054

Open
github-bot wants to merge 1 commit from github-bot/gh-2054 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 19:06 UTC · original
IlyassRS3:rt/lifecycle-fit-window-bounds-and-overflow → main · +44 −11 · 2 files · 1 commits · open

Claim ID: RT-LIFECYCLE-FIT-WINDOW-BOUNDS-AND-OVERFLOW

Summary

Hardens window fitting, centering, and registration in runtime/shims/lifecycle.cpp:

  1. Prevent zero-dimension truncation and floating-point overshoot in fit_window_to_area(): Extreme aspect ratios (e.g., 4000x2 or 2x4000 into a 1000x1000 work area) previously truncated the minor axis to 0, passing a zero-width or zero-height window to CreateWindowEx. Clamping both fitted axes to [1, min(area_span, INT_MAX)] guarantees valid non-zero dimensions that fit inside the work area while preserving exact results for normal aspect ratios (3840x2160 -> 1848x1040).
  2. Eliminate signed integer overflow UB in work-area spans and centering: Both fit_window_to_area() and prepare_title_window() now compute area.right - area.left, area.bottom - area.top, and centered (x, y) coordinates in std::int64_t before clamping to int.
  3. Harden register_app_state_change(): Skip ID 0 if g_next_reg_id ever wraps so a registration handle never collides with nullptr, and catch std::bad_alloc -> ERROR_NOT_ENOUGH_MEMORY instead of unwinding a C++ exception across WINAPI.
  4. Regression tests: Extends runtime/test/lifecycle_test.cpp with extreme wide/tall aspect ratio, wide virtual-desktop span (> INT_MAX), and empty work-area checks.

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind)
  • GPUs tested: none (verified locally on macOS arm64 compiling real runtime/shims/lifecycle.cpp with minimal Win32/shim stubs; Windows MSVC CI run of lifecycle pending)
  • Sanitizers & tests:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror: PASS (failures=0)
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror (4 threads x 500 concurrent register/unregister + suspend dispatch + window fit/centering): PASS (failures=0)
    • clang++ -std=c++20 -O2 -Wall -Wextra -Werror: PASS (failures=0)
    • Negative control on main under UBSan: triggered runtime error: signed integer overflow: 2000000000 - -2000000000 cannot be represented in type 'LONG' at lifecycle.cpp:208 and failed all 4 new assertions (main_exit=1).
  • Repository checks: python3 -I scripts/check_no_game_data.py clean; python3 -I tests/test_text_write_line_endings.py (3/3 OK).
**@⁠IlyassRS3** on GitHub · 2026-11-25 19:06 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2054) `IlyassRS3:rt/lifecycle-fit-window-bounds-and-overflow` → `main` · +44 −11 · 2 files · 1 commits · open Claim ID: `RT-LIFECYCLE-FIT-WINDOW-BOUNDS-AND-OVERFLOW` ### Summary Hardens window fitting, centering, and registration in `runtime/shims/lifecycle.cpp`: 1. **Prevent zero-dimension truncation and floating-point overshoot in `fit_window_to_area()`:** Extreme aspect ratios (e.g., `4000x2` or `2x4000` into a `1000x1000` work area) previously truncated the minor axis to `0`, passing a zero-width or zero-height window to `CreateWindowEx`. Clamping both fitted axes to `[1, min(area_span, INT_MAX)]` guarantees valid non-zero dimensions that fit inside the work area while preserving exact results for normal aspect ratios (`3840x2160` -> `1848x1040`). 2. **Eliminate signed integer overflow UB in work-area spans and centering:** Both `fit_window_to_area()` and `prepare_title_window()` now compute `area.right - area.left`, `area.bottom - area.top`, and centered `(x, y)` coordinates in `std::int64_t` before clamping to `int`. 3. **Harden `register_app_state_change()`:** Skip ID `0` if `g_next_reg_id` ever wraps so a registration handle never collides with `nullptr`, and catch `std::bad_alloc` -> `ERROR_NOT_ENOUGH_MEMORY` instead of unwinding a C++ exception across `WINAPI`. 4. **Regression tests:** Extends `runtime/test/lifecycle_test.cpp` with extreme wide/tall aspect ratio, wide virtual-desktop span (`> INT_MAX`), and empty work-area checks. ### Verification - **AI-assisted:** yes (Antigravity agent by Google DeepMind) - **GPUs tested:** none (verified locally on macOS arm64 compiling real `runtime/shims/lifecycle.cpp` with minimal Win32/shim stubs; Windows MSVC CI run of `lifecycle` pending) - **Sanitizers & tests:** - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror`: PASS (`failures=0`) - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror` (4 threads x 500 concurrent register/unregister + suspend dispatch + window fit/centering): PASS (`failures=0`) - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror`: PASS (`failures=0`) - Negative control on `main` under UBSan: triggered `runtime error: signed integer overflow: 2000000000 - -2000000000 cannot be represented in type 'LONG'` at `lifecycle.cpp:208` and failed all 4 new assertions (`main_exit=1`). - **Repository checks:** `python3 -I scripts/check_no_game_data.py` clean; `python3 -I tests/test_text_write_line_endings.py` (3/3 OK).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:16:13 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 19:06 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 19:06 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2054#pullrequestreview-6063396194) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2054/head:github-bot/gh-2054'
git switch 'github-bot/gh-2054'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2054'
git switch 'github-bot/gh-2054'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2054'
git switch 'github-bot/gh-2054'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2054'
git switch 'main'
git merge --squash 'github-bot/gh-2054'
git switch 'main'
git merge --ff-only 'github-bot/gh-2054'
git switch 'main'
git merge 'github-bot/gh-2054'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.