dev_overlay: avoid float-to-int overflow UB when formatting phase_for #2327

Merged
narekshaghoyandev merged 1 commit from upstream-pr/2327 into main 2026-11-28 18:10:40 +00:00
narekshaghoyandev commented 2026-11-26 13:50:54 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by narekshaghoyandev on 2026-11-26T13:50:54Z.


Fixes float-to-int overflow UB in leonida::dev_overlay::lines() (runtime/src/dev_overlay.h):

  • Previously, s.phase_for >= 1 ? (" for " + std::to_string(int(s.phase_for)) + " s").c_str() : "" cast double to int, invoking undefined behavior (float-cast-overflow) for values >= 2147483648.0 or +infinity and wrapping to -2147483648.
  • Guard with std::isfinite(s.phase_for) && s.phase_for >= 1.0 and format directly with std::snprintf(..., "now: %s for %.0f s", now[s.phase], std::floor(s.phase_for)), preserving truncation for normal values while eliminating both UB and the temporary std::string.
  • Added regression tests in runtime/test/dev_overlay_test.cpp.

Verification

  • g++ -std=c++20 -D_GLIBCXX_DEBUG -Wall -Wextra -Werror -pedantic -Iruntime/src runtime/test/dev_overlay_test.cpp passed
  • g++ -std=c++20 -fsanitize=address,undefined -Wall -Wextra -Werror -pedantic -Iruntime/src runtime/test/dev_overlay_test.cpp passed
  • Negative control: new test failed on line 168 (has(text, "now: refreshing textures for 1000000000000 s"), exit 1) against unmodified ubiq/main
  • python3 -I scripts/check_no_game_data.py runtime/src/dev_overlay.h runtime/test/dev_overlay_test.cpp clean

Claimed in the coordination issue as: RT-DEV-OVERLAY-PHASE-FOR-OVERFLOW (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7193877669)
AI-assisted: yes (Gemini)

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5511245983 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2327). Originally posted by [narekshaghoyandev](https://github.com/narekshaghoyandev) on 2026-11-26T13:50:54Z. --- Fixes float-to-int overflow UB in `leonida::dev_overlay::lines()` (`runtime/src/dev_overlay.h`): - Previously, `s.phase_for >= 1 ? (" for " + std::to_string(int(s.phase_for)) + " s").c_str() : ""` cast `double` to `int`, invoking undefined behavior (`float-cast-overflow`) for values `>= 2147483648.0` or `+infinity` and wrapping to `-2147483648`. - Guard with `std::isfinite(s.phase_for) && s.phase_for >= 1.0` and format directly with `std::snprintf(..., "now: %s for %.0f s", now[s.phase], std::floor(s.phase_for))`, preserving truncation for normal values while eliminating both UB and the temporary `std::string`. - Added regression tests in `runtime/test/dev_overlay_test.cpp`. ### Verification - `g++ -std=c++20 -D_GLIBCXX_DEBUG -Wall -Wextra -Werror -pedantic -Iruntime/src runtime/test/dev_overlay_test.cpp` passed - `g++ -std=c++20 -fsanitize=address,undefined -Wall -Wextra -Werror -pedantic -Iruntime/src runtime/test/dev_overlay_test.cpp` passed - Negative control: new test failed on line 168 (`has(text, "now: refreshing textures for 1000000000000 s")`, exit 1) against unmodified `ubiq/main` - `python3 -I scripts/check_no_game_data.py runtime/src/dev_overlay.h runtime/test/dev_overlay_test.cpp` clean Claimed in the coordination issue as: RT-DEV-OVERLAY-PHASE-FOR-OVERFLOW (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7193877669) AI-assisted: yes (Gemini)
lixonik commented 2026-11-26 15:44:04 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-26T15:44:04Z.

Review: COMMENTED


Ran the complete dev_overlay_test.cpp on Ubuntu/WSL with GCC 11 and both UBSan and float-cast-overflow checks enabled. It passed with no sanitizer report. No GPU or game input was required.

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6076965073 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2327#pullrequestreview-6076965073). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-26T15:44:04Z. **Review:** COMMENTED --- Ran the complete `dev_overlay_test.cpp` on Ubuntu/WSL with GCC 11 and both UBSan and float-cast-overflow checks enabled. It passed with no sanitizer report. No GPU or game input was required. AI-assisted.
zmasi commented 2026-11-28 12:15:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T12:15:59Z.


VERIFY-2327 — verified head cc091cd4cc merged cleanly with main d48a957bb on Windows 11 Pro (build 26200), MSVC 19.44 (x64, /std:c++20 /W4 /WX /permissive- /utf-8 /EHsc), synthetic overlay samples only.

Red (main d48a957bb) -> Green (d48a957bb + cc091cd4cc)

  • Red on main:
    • Compiling and running runtime/test/dev_overlay_test.cpp against main's runtime/src/dev_overlay.h -> FAILED (exit 1 at line 168: has(text, "now: refreshing textures for 1000000000000 s") due to int(s.phase_for) float-to-int overflow on 1e12).
  • Green on merged PR:
    • cl.exe /nologo /std:c++20 /W4 /WX /permissive- /utf-8 /EHsc /I"runtime/src" runtime/test/dev_overlay_test.cpp -> 0 warnings, exit 0 (dev_overlay_test: ok).

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7239107911 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2327#issuecomment-7239107911). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T12:15:59Z. --- `VERIFY-2327` — verified head `cc091cd4cc` merged cleanly with `main` `d48a957bb` on Windows 11 Pro (build 26200), MSVC `19.44` (`x64`, `/std:c++20 /W4 /WX /permissive- /utf-8 /EHsc`), synthetic overlay samples only. ### Red (`main` `d48a957bb`) -> Green (`d48a957bb` + `cc091cd4cc`) - **Red on `main`**: - Compiling and running `runtime/test/dev_overlay_test.cpp` against `main`'s `runtime/src/dev_overlay.h` -> **FAILED** (`exit 1` at `line 168: has(text, "now: refreshing textures for 1000000000000 s")` due to `int(s.phase_for)` float-to-int overflow on `1e12`). - **Green on merged PR**: - `cl.exe /nologo /std:c++20 /W4 /WX /permissive- /utf-8 /EHsc /I"runtime/src" runtime/test/dev_overlay_test.cpp` -> **0 warnings**, exit **`0`** (`dev_overlay_test: ok`). AI-assisted.
MoonEyes commented 2026-11-28 18:10:37 +00:00 (Migrated from github.com)

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


Red/green: On main dev_overlay_test fails at line 168 (a huge phase duration overflows the float-to-int conversion and prints 1000000000000 s), and with the PR ctest -R "^dev_overlay$" 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:7243589157 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2327#issuecomment-7243589157). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T18:10:37Z. --- Red/green: On main dev_overlay_test fails at line 168 (a huge phase duration overflows the float-to-int conversion and prints 1000000000000 s), and with the PR ctest -R "^dev_overlay$" 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 22:02:04 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T22:02:04Z.


Verified by OreoFR

  • Base: main c1ecf04ed + PR head cc091cd4c; green run in a merge package of 16 PRs, no new ctest failure vs main
  • Test: ctest -R "^dev_overlay$"
  • Red on main: 1 failure, line 168 ("refreshing textures for 1000000000000 s")
  • 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:7246448652 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2327#issuecomment-7246448652). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T22:02:04Z. --- **Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `cc091cd4c`; green run in a merge package of 16 PRs, no new ctest failure vs main - Test: `ctest -R "^dev_overlay$"` - Red on main: 1 failure, line 168 ("refreshing textures for 1000000000000 s") - 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!2327
No description provided.