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

Merged
IlyassRS3 merged 1 commit from upstream-pr/2054 into main 2026-11-28 18:14:05 +00:00
IlyassRS3 commented 2026-11-25 19:06:14 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T19:06:14Z.


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).
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5497444558 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2054). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T19:06:14Z. --- 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).
copilot-pull-request-reviewer[bot] commented 2026-11-25 19:06:26 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T19:06:26Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6063396194 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2054#pullrequestreview-6063396194). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T19:06:26Z. **Review:** COMMENTED --- 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.
zmasi commented 2026-11-28 11:28:35 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T11:28:35Z.


Verified on Windows 11 (10.0.26200.9974) with MSVC 19.44.35228 (x64, /std:c++20 /EHsc /W4 /permissive- /utf-8 /O2) against main 4a54aedcb + 892f62708 (claim #2570#issuecomment-7238486276):

  • Line-by-line diff audit: Clean (runtime/shims/lifecycle.cpp, runtime/test/lifecycle_test.cpp); widens area_w/area_h and centering math to int64_t with int clamping, clamps scaled window axes to >= 1, skips 0 on g_next_reg_id wraparound, and catches std::bad_alloc -> ERROR_NOT_ENOUGH_MEMORY. check_no_game_data.py clean.
  • Red (4a54aedcb + PR test): lifecycle_test.exe fails (rc=1, 34 check(s), 3 failure(s) on extreme wide/tall minor-axis clamping and >32-bit work-area span overflow).
  • Green (4a54aedcb + 892f62708 merge): Merges cleanly (rc=0), compiles with 0 /W4 warnings, and lifecycle_test.exe passes (34 check(s), 0 failure(s), rc=0).

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7238576364 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2054#issuecomment-7238576364). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T11:28:35Z. --- Verified on Windows 11 (`10.0.26200.9974`) with MSVC `19.44.35228` (`x64`, `/std:c++20 /EHsc /W4 /permissive- /utf-8 /O2`) against `main` `4a54aedcb` + `892f62708` (claim `#2570#issuecomment-7238486276`): - **Line-by-line diff audit**: Clean (`runtime/shims/lifecycle.cpp`, `runtime/test/lifecycle_test.cpp`); widens `area_w`/`area_h` and centering math to `int64_t` with `int` clamping, clamps scaled window axes to `>= 1`, skips `0` on `g_next_reg_id` wraparound, and catches `std::bad_alloc` -> `ERROR_NOT_ENOUGH_MEMORY`. `check_no_game_data.py` clean. - **Red (`4a54aedcb` + PR test)**: `lifecycle_test.exe` fails (`rc=1`, `34 check(s), 3 failure(s)` on extreme wide/tall minor-axis clamping and >32-bit work-area span overflow). - **Green (`4a54aedcb` + `892f62708` merge)**: Merges cleanly (`rc=0`), compiles with `0` `/W4` warnings, and `lifecycle_test.exe` passes (`34 check(s), 0 failure(s)`, `rc=0`). AI-assisted.
MoonEyes commented 2026-11-28 18:14:02 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T18:14:02Z.


Red/green: On main lifecycle_test fails 3 checks (extreme aspect ratios and a work area wider than signed 32 bits are mishandled), and with the PR ctest -R "^lifecycle$" passes.
Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7243633546 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2054#issuecomment-7243633546). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T18:14:02Z. --- Red/green: On main lifecycle_test fails 3 checks (extreme aspect ratios and a work area wider than signed 32 bits are mishandled), and with the PR ctest -R "^lifecycle$" passes. Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.
OreoFR commented 2026-11-28 21:59:11 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T21:59:11Z.


Verified by OreoFR

  • Base: main c1ecf04ed + PR head 892f62708; green run in a merge package of 16 PRs, no new ctest failure vs main
  • Test: ctest -R "^lifecycle$"
  • Red on main: 3 failures (extreme aspect ratio, work area beyond signed 32 bits)
  • Green with the PR: OK
  • Verdict: READY
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246414313 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2054#issuecomment-7246414313). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T21:59:11Z. --- **Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `892f62708`; green run in a merge package of 16 PRs, no new ctest failure vs main - Test: `ctest -R "^lifecycle$"` - Red on main: 3 failures (extreme aspect ratio, work area beyond signed 32 bits) - Green with the PR: OK - Verdict: READY - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!2054
No description provided.