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

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

@⁠narekshaghoyandev on GitHub · 2026-11-26 13:50 UTC · original
narekshaghoyandev:ubiq-dev-overlay-phase-for → main · +17 −4 · 2 files · 1 commits · open

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 (#1608)
AI-assisted: yes (Gemini)

**@⁠narekshaghoyandev** on GitHub · 2026-11-26 13:50 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2327) `narekshaghoyandev:ubiq-dev-overlay-phase-for` → `main` · +17 −4 · 2 files · 1 commits · open 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://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) AI-assisted: yes (Gemini)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 13:55:51 +00:00
Author
Owner

@⁠lixonik on GitHub · 2026-11-26 15:44 UTC · original · reviewed

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.

**@⁠lixonik** on GitHub · 2026-11-26 15:44 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2327#pullrequestreview-6076965073) · **reviewed** 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.
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/2327/head:github-bot/gh-2327'
git switch 'github-bot/gh-2327'

Merge

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