From 1394637ce87755f9d1f2673a806b24dc8624f518 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sun, 18 Sep 2022 23:49:58 +0300 Subject: [PATCH] server: func_tank: prevent domain error on barrel adjust (#328) When target is too close to the tank origin, `d2 - r2` expression may become negative causing domain error on square root, and poisoning other fields and even other entities with NaN --- dlls/func_tank.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dlls/func_tank.cpp b/dlls/func_tank.cpp index fd6ff1e7..c9945848 100644 --- a/dlls/func_tank.cpp +++ b/dlls/func_tank.cpp @@ -626,12 +626,14 @@ void CFuncTank::AdjustAnglesForBarrel( Vector &angles, float distance ) if( m_barrelPos.y ) { r2 = m_barrelPos.y * m_barrelPos.y; - angles.y += ( 180.0f / M_PI_F ) * atan2( m_barrelPos.y, sqrt( d2 - r2 ) ); + if( d2 > r2 ) + angles.y += ( 180.0f / M_PI_F ) * atan2( m_barrelPos.y, sqrt( d2 - r2 ) ); } if( m_barrelPos.z ) { r2 = m_barrelPos.z * m_barrelPos.z; - angles.x += ( 180.0f / M_PI_F ) * atan2( -m_barrelPos.z, sqrt( d2 - r2 ) ); + if( d2 > r2 ) + angles.x += ( 180.0f / M_PI_F ) * atan2( -m_barrelPos.z, sqrt( d2 - r2 ) ); } } }