From 0a3a52e54e8bb40e107a3b92c32e42a866c49f3b Mon Sep 17 00:00:00 2001 From: "(Jip) Willem Wijnia" Date: Fri, 9 Aug 2024 11:15:52 +0200 Subject: [PATCH] Fix being able to pass through invalid ballistic values (#6398) ## Description of the proposed changes If you pass in an invalid value to `SetBallisticAcceleration` then the simulation will freeze up. This is a bug in the engine, but we can 'fix' it in Lua by simply error'ing out if we see such a value. ## Testing done on the proposed changes Ran a game and tested/confirmed that regular values are untouched and all variants of invalid values are caught. ## Additional context Possibly related to: - https://forum.faforever.com/topic/8022/replays-of-games-stuck-in-infinite-loop/1 ## Checklist - [x] Changes are annotated, including comments where useful - [x] Changes are documented in the changelog for the next game version --- changelog/snippets/fix.6398.md | 3 +++ lua/sim/Projectile.lua | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 changelog/snippets/fix.6398.md diff --git a/changelog/snippets/fix.6398.md b/changelog/snippets/fix.6398.md new file mode 100644 index 0000000000..d9f65d1536 --- /dev/null +++ b/changelog/snippets/fix.6398.md @@ -0,0 +1,3 @@ +- (#6398) Fix a possible cause for a simulation freeze + +It was possible to pass invalid numbers (NaN or infinite) as a ballistic acceleration for a projectile. This would cause the engine to freeze up. With these changes we introduce Lua guards to catch the invalid numbers and throw an error instead of passing the invalid number to the engine. diff --git a/lua/sim/Projectile.lua b/lua/sim/Projectile.lua index 71df80d9cd..c481e8aca9 100644 --- a/lua/sim/Projectile.lua +++ b/lua/sim/Projectile.lua @@ -30,6 +30,7 @@ local ProjectileMethods = moho.projectile_methods local ProjectileMethodsCreateChildProjectile = ProjectileMethods.CreateChildProjectile local ProjectileMethodsGetMaxZigZag = ProjectileMethods.GetMaxZigZag local ProjectileMethodsGetZigZagFrequency = ProjectileMethods.GetZigZagFrequency +local ProjectileMethodsSetBallisticAcceleration = ProjectileMethods.SetBallisticAcceleration local EntityMethods = _G.moho.entity_methods local EntityGetBlueprint = EntityMethods.GetBlueprint @@ -996,6 +997,28 @@ Projectile = ClassProjectile(ProjectileMethods, DebugProjectileComponent) { return frequency end, + --- Set the vertical (gravitational) acceleration of the projectile. Default is -4.9, which is expected by the engine's weapon targeting and firing + ---@param acceleration number + SetBallisticAcceleration = function(self, acceleration) + + -- Fix an engine bug where the values `1.#INF` or `-1.#IND` passed + -- into this particular engine function can cause the simulation to freeze up. + -- + -- Since `math.huge` does not exist (and does not cover the #IND case) I see + -- no other approach than this to try and 'fix' it. + -- + -- Related sources: + -- - https://stackoverflow.com/questions/19107302/in-lua-what-is-inf-and-ind + + -- guard to prevent invalid numbers (#IND) and infinite numbers (#INF) from reaching the engine function + local stringified = tostring(acceleration) + if stringified:find('#') then + error("Invalid acceleration value: " .. stringified) + end + + return ProjectileMethodsSetBallisticAcceleration(self, acceleration) + end, + --------------------------------------------------------------------------- --#region Deprecated functionality