From d9cf939ba1f2cfa40feb77c4923694e2a8556204 Mon Sep 17 00:00:00 2001 From: Roberto Fernandez Bautista Date: Thu, 12 Aug 2021 16:32:06 +0100 Subject: [PATCH] SHAPE_LINE_CHAIN::Replace Don't call front() or back() if empty Fixes https://gitlab.com/kicad/code/kicad/-/issues/8949 --- libs/kimath/src/geometry/shape_line_chain.cpp | 17 ++++++++++- .../kimath/geometry/test_shape_line_chain.cpp | 29 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/libs/kimath/src/geometry/shape_line_chain.cpp b/libs/kimath/src/geometry/shape_line_chain.cpp index 7f73a86aae..d21697ee8c 100644 --- a/libs/kimath/src/geometry/shape_line_chain.cpp +++ b/libs/kimath/src/geometry/shape_line_chain.cpp @@ -478,11 +478,25 @@ void SHAPE_LINE_CHAIN::Replace( int aStartIndex, int aEndIndex, const SHAPE_LINE SHAPE_LINE_CHAIN newLine = aLine; + // Zero points to add? + if( newLine.PointCount() == 0 ) + { + Remove( aStartIndex, aEndIndex ); + return; + } + // Remove coincident points in the new line if( newLine.m_points.front() == m_points[aStartIndex] ) { aStartIndex++; newLine.Remove( 0 ); + + // Zero points to add? + if( newLine.PointCount() == 0 ) + { + Remove( aStartIndex, aEndIndex ); + return; + } } if( newLine.m_points.back() == m_points[aEndIndex] && aEndIndex > 0 ) @@ -493,7 +507,8 @@ void SHAPE_LINE_CHAIN::Replace( int aStartIndex, int aEndIndex, const SHAPE_LINE Remove( aStartIndex, aEndIndex ); - if( !newLine.PointCount() ) + // Zero points to add? + if( newLine.PointCount() == 0 ) return; // The total new arcs index is added to the new arc indices diff --git a/qa/libs/kimath/geometry/test_shape_line_chain.cpp b/qa/libs/kimath/geometry/test_shape_line_chain.cpp index 7c3acdc873..bab11efb52 100644 --- a/qa/libs/kimath/geometry/test_shape_line_chain.cpp +++ b/qa/libs/kimath/geometry/test_shape_line_chain.cpp @@ -290,5 +290,34 @@ BOOST_AUTO_TEST_CASE( NearestPointPt ) } +// Test SHAPE_LINE_CHAIN::Replace( SHAPE_LINE_CHAIN ) +BOOST_AUTO_TEST_CASE( ReplaceChain ) +{ + BOOST_TEST_INFO( "8949 crash" ); + + std::vector linePts = { + { 206000000, 140110000 }, { 192325020, 140110000 }, { 192325020, 113348216 }, + { 192251784, 113274980 }, { 175548216, 113274980 }, { 175474980, 113348216 }, + { 175474980, 136694980 }, { 160774511, 121994511 }, { 160774511, 121693501 }, + { 160086499, 121005489 }, { 159785489, 121005489 }, { 159594511, 120814511 }, + { 160086499, 120814511 }, { 160774511, 120126499 }, { 160774511, 119153501 }, + { 160086499, 118465489 }, { 159113501, 118465489 }, { 158425489, 119153501 }, + { 158425489, 119645489 }, { 157325020, 118545020 }, { 157325020, 101925020 }, + { 208674980, 101925020 }, { 208674980, 145474980 }, { 192325020, 145474980 }, + { 192325020, 140110000 } + }; + + SHAPE_LINE_CHAIN baseChain( linePts, false ); + baseChain.SetWidth( 250000 ); + BOOST_CHECK_EQUAL( baseChain.PointCount(), linePts.size() ); + + SHAPE_LINE_CHAIN replaceChain( { VECTOR2I( 192325020, 140110000 ) }, false ); + BOOST_CHECK_EQUAL( replaceChain.PointCount(), 1 ); + + baseChain.Replace( 1, 23, replaceChain ); + + BOOST_CHECK_EQUAL( baseChain.PointCount(), linePts.size() - ( 23 - 1 ) ); +} + BOOST_AUTO_TEST_SUITE_END()