Fix a crash when running 'snap geometries to layer'

A crash may happen when running the alg with 'snap to anchor nodes (single layer only)' behavior.
It turns out that I made a mistake while porting the algorithm to std::sort - originally
the sorting function used -1/0/+1 for comparison, while std::sort wants '<' operator returning true/false.
Due to inconsistent results from the comparison function, std::sort would end up corrupting
the array and memory even beyond the range, causing crashes.

Related to #29400 (but does not fix it)
This commit is contained in:
Martin Dobias 2019-06-06 16:21:43 +02:00
parent 2a9028ad28
commit 0ba99af486

View File

@ -244,9 +244,9 @@ static bool snapLineString( QgsLineString *linestring, QgsSpatialIndex &index, Q
if ( !newVerticesAlongSegment.isEmpty() )
{
// sort by distance along the segment
std::sort( newVerticesAlongSegment.begin(), newVerticesAlongSegment.end(), []( AnchorAlongSegment p1, AnchorAlongSegment p2 )
std::sort( newVerticesAlongSegment.begin(), newVerticesAlongSegment.end(), []( const AnchorAlongSegment & p1, const AnchorAlongSegment & p2 )
{
return ( p1.along < p2.along ? -1 : ( p1.along > p2.along ) );
return p1.along < p2.along;
} );
// insert new vertices