Crash when a Lua Hit handler reads attack.weapon for a thrown weapon
Issue Description
When an actor is hit by a thrown weapon (throwing star, dart, knife), any local script whose I.Combat hit handler reads attack.weapon crashes the game (signal 11). No Lua error is logged. The same happens with an arrow or bolt
whose launcher was unequipped while the projectile was flying.
This follows up on #8664 (closed) / !4879 (merged). The description of !4879 (merged) says that for thrown weapons "weapon is always going to be an invalid object", but the object is not invalid. It dangles:
- In
ProjectileManager::moveProjectiles(apps/openmw/mwworld/projectilemanager.cpp),bowisprojectileRef.getPtr(), aManualRefon the stack, unless the launcher is still equipped. For thrown weapons it always is theManualRef. It is passed as bothweaponandprojectiletoMWMechanics::projectileHit, and then toLuaManager::onHit. onHitwraps it inLObject(weapon). TheSafePtrconstructor stores the rawPtrtogether with the currentgetPtrRegistryRevision().- The
ManualRefis destroyed at the end of the loop iteration, before the queued localHitevent reaches the handlers. - In the handler,
SafePtr::update()only re-resolves thePtrif the registry revision has changed. Usually it has not, soptrOrEmpty()returns the danglingPtr, and any access (recordId,types.Weapon.objectIsInstance,types.Weapon.record) is a use-after-free.
The engine's own _onHit is safe because it reads ammo first (apps/openmw/mwlua/types/actor.cpp). Mods that look at the weapon of a ranged hit, for example to find its skill or type, crash instead.
A possible fix: in LuaManager::onHit, don't pass weapon when it is the projectile itself (weapon == ammo, which covers both thrown weapons and ammunition without a launcher). Alternatively, pass it as a record id, as is already
done for ammo.
Steps to Reproduce
- Add a content file with this script, attached to actors:
test.omwscripts:NPC, CREATURE: scripts/hitcrash.luascripts/hitcrash.lua:local I = require('openmw.interfaces') I.Combat.addOnHitHandler(function(attack) if attack.weapon then print(attack.weapon.recordId) end end)
- Equip a stack of throwing stars (e.g.
player->additem chitin throwing star 20). - Throw one at an NPC or a creature and hit it.
- The game crashes with signal 11.
Melee hits, and arrows or bolts shot from a launcher that is still equipped, print the weapon id as expected.
System Information
- OpenMW Version: 0.52.0 RC1 (4ff261bf). The same code is on
master(77eec047), and has been since !4879 (merged) (0.50). - Operating System & Version: Linux (Ubuntu)
- GPU: Intel Graphics
- Morrowind Version: CD
- Language: English
- Addons: Tribunal, Bloodmoon, Expanded Vanilla
Mods
- Are you using mods? Yes. Found with a Lua mod (Natural Character Growth) whose hit handler read
attack.weapon. The script above reproduces it without that mod. - Are you using any shader mods? Yes
Logs and Settings
openmw.log ends with no error. The crash handler only reports Fatal Error (signal 11), Address: (nil), with no backtrace (gdb timed out while downloading debug info).