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

Open
opened 2026-11-26 21:19:53 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2054
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: rt/lifecycle-fit-window-bounds-and-overflow


📝 Commits (1)

  • 892f627 runtime: guard window-fit zero dimensions, signed overflow, and OOM in lifecycle shim

📊 Changes

2 files changed (+44 additions, -11 deletions)

View changed files

📝 runtime/shims/lifecycle.cpp (+30 -11)
📝 runtime/test/lifecycle_test.cpp (+14 -0)

📄 Description

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).

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2054 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `rt/lifecycle-fit-window-bounds-and-overflow` --- ### 📝 Commits (1) - [`892f627`](https://github.com/ubiquitarianism/leonida/commit/892f6270827f51ff7961c25d04cdba37410f1a97) runtime: guard window-fit zero dimensions, signed overflow, and OOM in lifecycle shim ### 📊 Changes **2 files changed** (+44 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `runtime/shims/lifecycle.cpp` (+30 -11) 📝 `runtime/test/lifecycle_test.cpp` (+14 -0) </details> ### 📄 Description 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). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
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
starred/leonida#2060
No description provided.