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

Merged
leonida/mandy
narekshaghoyandev merged 1 commit from ubiq-dev-overlay-phase-for into main 2026-11-28 18:10:40 +00:00

Pull request successfully merged and closed

narekshaghoyandev commented 2026-11-26 13:50:54 +00:00

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)

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 reviewed 2026-11-26 15:44:04 +00:00
lixonik left a comment
No description provided.
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

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.

`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

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.

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

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

**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 description provided.