Safety valve for integer sqrt

If the value is negative, we have likely overflowed the available size
of the integer container.  In which case, rather than slowly counting
down, we return the largest logical square root size to allow processing
to continue

Fixes https://gitlab.com/kicad/code/kicad/-/issues/20947

(cherry picked from commit b47bf99632c8faae8855a86a0a30ef233fd594db)
This commit is contained in:
Seth Hillbrand 2025-05-20 15:10:44 -07:00
parent 8ad9634498
commit 9508979040

View File

@ -63,6 +63,9 @@ T isqrt(T x)
T r = (T) std::sqrt((double) x);
T sqrt_max = sqrt_max_typed<T>;
if( x < 0 )
return sqrt_max;
while (r < sqrt_max && r * r < x)
r++;
while (r > sqrt_max || r * r > x)
@ -474,7 +477,7 @@ SEG::ecoord SEG::SquaredDistance( const VECTOR2I& aP ) const
// e is the projection of aP onto ab and therefore cannot be greater than
// the length of ap and f is guaranteed to be greater than e, meaning
// e * e / f cannot be greater than ap.SquaredEuclideanNorm()
if( g < 0 )
if( g < 0 || g > static_cast<double>( std::numeric_limits<ecoord>::max() ) )
return 0;
return KiROUND<double, ecoord>( g );