Skip to content

Fix client crash on invalid setTrainTrack track number#5049

Open
HeresHavi wants to merge 1 commit into
multitheftauto:masterfrom
HeresHavi:fix-set-train-track-invalid-track-crash
Open

Fix client crash on invalid setTrainTrack track number#5049
HeresHavi wants to merge 1 commit into
multitheftauto:masterfrom
HeresHavi:fix-set-train-track-invalid-track-crash

Conversation

@HeresHavi

@HeresHavi HeresHavi commented Jul 13, 2026

Copy link
Copy Markdown

Summary

Changed setTrainTrack to throw std::invalid_argument by value.

Invalid track numbers are now caught by the existing Lua function parser and returned as a normal Lua error instead of terminating the client.

Motivation

setTrainTrack previously used throw new std::invalid_argument(...), which throws a pointer to an exception. The Lua function parser catches std::logic_error&, so the pointer escaped the handler and became a fatal C++ exception.

For example, a train resource might read the track number from a route configuration. If that value is 4 instead of the valid range from 0 through 3, calling setTrainTrack could crash the player's client.

Throwing the exception object by value lets the existing handler report the invalid argument normally.

Crash Stack
RaiseException
_CxxThrowException
CLuaVehicleDefs::SetTrainTrack (CLuaVehicleDefs.cpp:2369)
CLuaFunctionParser<..., &CLuaVehicleDefs::SetTrainTrack>::Call
CLuaFunctionParser<..., &CLuaVehicleDefs::SetTrainTrack>::operator()
CLuaDefs::ArgumentParser<&CLuaVehicleDefs::SetTrainTrack>
luaD_precall
luaV_execute
Crash

Test plan

Runtime

  • Setup: Created a local Freight train using model 537.
  • Valid Case: setTrainTrack(train, 0) returned call=true result=true before and after the fix.
  • Before Fix: setTrainTrack(train, 4) caused a fatal C++ exception in CLuaVehicleDefs::SetTrainTrack.
  • After Fix: The same call returned call=false result=Invalid track number range (0-3), and the client stayed open.

Builds and Tests

  • Debug | Win32: Full build passed, 304 client tests passed.
  • Release | Win32: Full build passed, 304 client tests passed.
  • Debug | x64: Full build passed.
  • Release | x64: Full build passed.
  • Ran clang-format.

Checklist

  • Your code should follow the coding guidelines.
  • Smaller pull requests are easier to review. If your pull request is beefy, your pull request should be reviewable commit-by-commit.

Throw invalid train track errors by value so the Lua function parser can handle them instead of terminating the client.
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.

1 participant