[PR #2327] dev_overlay: avoid float-to-int overflow UB when formatting phase_for #2333

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

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2327
Author: @narekshaghoyandev
Created: 11/26/2026
Status: 🔄 Open

Base: main ← Head: ubiq-dev-overlay-phase-for


📝 Commits (1)

  • cc091cd dev_overlay: avoid float-to-int overflow UB when formatting phase_for

📊 Changes

2 files changed (+17 additions, -4 deletions)

View changed files

📝 runtime/src/dev_overlay.h (+8 -4)
📝 runtime/test/dev_overlay_test.cpp (+9 -0)

📄 Description

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)


🔄 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/2327 **Author:** [@narekshaghoyandev](https://github.com/narekshaghoyandev) **Created:** 11/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `ubiq-dev-overlay-phase-for` --- ### 📝 Commits (1) - [`cc091cd`](https://github.com/ubiquitarianism/leonida/commit/cc091cd4ccec2e3ebc10931a69f20f0fbe5cf979) dev_overlay: avoid float-to-int overflow UB when formatting phase_for ### 📊 Changes **2 files changed** (+17 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/dev_overlay.h` (+8 -4) 📝 `runtime/test/dev_overlay_test.cpp` (+9 -0) </details> ### 📄 Description 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) --- <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#2333
No description provided.