Skip to content

Fix: handle over/underflows in (int) TLuaInterpreter::getVerifiedInt(…)#8924

Merged
SlySven merged 6 commits intoMudlet:developmentfrom
SlySven:Fix_handleOverflowsIn_TLuaInterpreter_getVerifiedIn
Apr 4, 2026
Merged

Fix: handle over/underflows in (int) TLuaInterpreter::getVerifiedInt(…)#8924
SlySven merged 6 commits intoMudlet:developmentfrom
SlySven:Fix_handleOverflowsIn_TLuaInterpreter_getVerifiedIn

Conversation

@SlySven
Copy link
Copy Markdown
Member

@SlySven SlySven commented Feb 8, 2026

Brief overview of PR changes/additions

Revises (int) TLuaInterpreter::getVerifiedInt(...) so it checks and errors out should the call of lua_tointeger(...) return a value outside the range that can be conveyed as an int (actually int32_t). This function actually returns a lua_Integer which is a typedef of ptrdiff_t - which is a signed 32-bit integer on 32-Bit Windows OS but is actually a signed 64-bit integer on all the platforms we currently support.

Motivation for adding to Mudlet

Prevent odd behaviour in the event of integer over/underflows.

Other info (issues closed, discussion etc)

This came about from the changes in 3609e94 as part of #4661 in 2021 (which seemes to assume getVerifiedInt(...) did actually return an int) - ironically I had actually fixed the conversion of 64-bit to 32-bit integers for exit weights in the earlier #2106 but which that PR undid.

…...)

#### Brief overview of PR changes/additions
Revises `(int) TLuaInterpreter::getVerifiedInt(...)` so it checks and
errors out should the call of `lua_tointeger(...)` return a value outside
the range that can be conveyed as an `int` (actually `int32_t`). This
function actaully returns a `lua_Integer` which is a `typedef` of
`ptrdiff_t` - which is a signed 32-bit integer on 32-Bit Windows OS but is
actually a signed 64-bit integer on all the platforms we currently support.

#### Motivation for adding to Mudlet
Prevent odd behaviour in the event of integer over/underflows.

#### Other info (issues closed, discussion etc)
This came about from the changes in 3609e94
as part of Mudlet#4661 in 2021 (which seemes to assume `getVerifiedInt(...)` did
actually return an `int`) - ironically I had actually fixed the conversion
of 64-bit to 32-bit integers for exit weights in the earlier Mudlet#2106 but
which that PR undid.

Signed-off-by: Stephen Lyons <slysven@virginmedia.com>
@SlySven SlySven requested a review from a team as a code owner February 8, 2026 02:36
@add-deployment-links
Copy link
Copy Markdown

add-deployment-links bot commented Feb 8, 2026

Hey there! Thanks for helping Mudlet improve. 🌟

Test versions

You can directly test the changes here:

No need to install anything - just unzip and run.
Let us know if it works well, and if it doesn't, please give details.

@SlySven
Copy link
Copy Markdown
Member Author

SlySven commented Feb 8, 2026

Behaviour prior to this PR with a too large positive integer:
image

and a too large negative one:
image

after this PR:
image

@vadi2
Copy link
Copy Markdown
Member

vadi2 commented Mar 21, 2026

PR #8924 Review

Core overflow check in getVerifiedInt() looks good - std::numeric_limits, explicit static_cast, correct ptrdiff_t claim per Lua 5.1 headers. Discord callsite cleanup correctly removes redundant qMin/static_cast clamping. Protects all ~320 existing getVerifiedInt() callsites from silent truncation.

Two suggestions:

  1. Overflow error message (TLuaInterpreter.cpp:235) shows the valid range but not the value the user passed. Including offending values is the established pattern in the codebase. Since lua_pushfstring doesn't support 64-bit format specifiers, QByteArray::number() with %s would work.

  2. Comment at TLuaInterpreterNetworking.cpp:79-81 about lua_tointeger() returning 64-bit values is now stale since connectToServer() uses getVerifiedInt() which handles overflow internally.

Pre-existing observations (not from this PR, potential follow-ups):

  • ~17 raw lua_tointeger -> int truncation sites across TLuaInterpreterMudletObjects.cpp, TLuaInterpreterMapper.cpp, and TLuaInterpreterAI.cpp bypass getVerifiedInt() and could silently truncate.

  • getVerifiedStringOrInteger has the same class of overflow bug via qRound(lua_tonumber()), though practical risk is low as it's only used for small Mudlet object IDs.

@ZookaOnGit ZookaOnGit added this to the 4.21.0 milestone Mar 25, 2026
@SlySven
Copy link
Copy Markdown
Member Author

SlySven commented Apr 4, 2026

Applied suggestions for THIS PR:
image

@SlySven SlySven enabled auto-merge (squash) April 4, 2026 02:32
@SlySven SlySven merged commit f0b60a1 into Mudlet:development Apr 4, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants