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
This commit is contained in:
Seth Hillbrand
2025-05-20 15:14:07 -07:00
parent de5f65d18e
commit b47bf99632
+4 -1
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)
@@ -469,7 +472,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 );