From 9cb6df9c383c511c04496ffc1abe31c96ec4e478 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Tue, 13 Sep 2022 13:27:39 +0100 Subject: [PATCH] Don't crash on empty SHAPE_LINE_CHAINs. Fixes https://gitlab.com/kicad/code/kicad/issues/12407 (cherry picked from commit fc74de81fb8279911c87aa352bc0b9a375c8cb90) --- libs/kimath/src/geometry/shape_line_chain.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libs/kimath/src/geometry/shape_line_chain.cpp b/libs/kimath/src/geometry/shape_line_chain.cpp index 723ee2391a..5c225ac881 100644 --- a/libs/kimath/src/geometry/shape_line_chain.cpp +++ b/libs/kimath/src/geometry/shape_line_chain.cpp @@ -1791,6 +1791,12 @@ SHAPE_LINE_CHAIN& SHAPE_LINE_CHAIN::Simplify( bool aRemoveColinear ) const VECTOR2I SHAPE_LINE_CHAIN::NearestPoint( const VECTOR2I& aP, bool aAllowInternalShapePoints ) const { + if( PointCount() == 0 ) + { + // The only right answer here is "don't crash". + return { 0, 0 }; + } + int min_d = INT_MAX; int nearest = 0; @@ -1842,6 +1848,12 @@ const VECTOR2I SHAPE_LINE_CHAIN::NearestPoint( const VECTOR2I& aP, const VECTOR2I SHAPE_LINE_CHAIN::NearestPoint( const SEG& aSeg, int& dist ) const { + if( PointCount() == 0 ) + { + // The only right answer here is "don't crash". + return { 0, 0 }; + } + int nearest = 0; dist = INT_MAX;