Commit 5fbda679 authored by Menno Deij - van Rijswijk's avatar Menno Deij - van Rijswijk
Browse files

fixed a corner-case out-of-memory crash in polygon builder

parent cea55385
......@@ -4,6 +4,7 @@ vtk_add_test_cxx(${vtk-module}CxxTests tests
TestPolygonBuilder2.cxx
TestPolygonBuilder3.cxx
TestPolygonBuilder4.cxx
TestPolygonBuilder5.cxx
UnitTestFunctionParser.cxx
)
vtk_test_cxx_executable(${vtk-module}CxxTests tests)
/*=========================================================================
Program: Visualization Toolkit
Module: TestCutter.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkIdListCollection.h"
#include "vtkNew.h"
#include "vtkPoints.h"
#include "vtkPolygonBuilder.h"
#include "vtkSmartPointer.h"
int TestPolygonBuilder5(int, char* [])
{
// this case comes from a real-world example (see https://gitlab.kitware.com/vtk/vtk/issues/17170 )
#define NTRIANGLES 7
vtkIdType tris[NTRIANGLES][3] =
{ {0,1,2},{3,4,7},{7,4,5},{4,6,5},{3,7,4},{4,7,6},{7,5,6} };
vtkPolygonBuilder builder;
vtkIdType p[3];
for(int i = 0; i < NTRIANGLES; ++i)
{
for(int j = 0; j < 3; ++j)
p[j] = tris[i][j];
builder.InsertTriangle(p);
}
vtkNew<vtkIdListCollection> polys;
builder.GetPolygons(polys.GetPointer()); // will result in out-of-memory crash
if (polys->GetNumberOfItems() < 1)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
......@@ -13,6 +13,7 @@
=========================================================================*/
#include "vtkPolygonBuilder.h"
#include "vtkNew.h"
#include "vtkIdListCollection.h"
vtkPolygonBuilder::vtkPolygonBuilder()
......@@ -128,7 +129,7 @@ void vtkPolygonBuilder::GetPolygons(vtkIdListCollection* polys)
while (!(this->Edges.empty()))
{
vtkIdList* poly = vtkIdList::New();
vtkNew<vtkIdList> poly;
EdgeMap::iterator edgeIt = this->Edges.begin();
Edge edge = *(edgeIt);
......@@ -138,17 +139,21 @@ void vtkPolygonBuilder::GetPolygons(vtkIdListCollection* polys)
do
{
poly->InsertNextId(edge.first);
edgeIt = this->Edges.find(edge.second);
EdgeMap::iterator at = this->Edges.find(edge.second);
// ignore polygon if Edges map not correct - with the fixes for
// ignoring collapsed triangles and ignoring duplicate triangles
// this should not happen anymore, but it does not hurt to be safe.
if (edgeIt == this->Edges.end())
//
// UPDATE: from a real-world polyhedron case it IS possible to
// get dangling edges. See TestPolygonBuilder5 for details.
if (at == this->Edges.end())
{
Edges.erase(edgeIt); //remove offending edge
poly->Reset(); // empty the list so it does not get added below
break;
}
edge = *(edgeIt);
Edges.erase(edgeIt);
edge = *(at);
Edges.erase(at);
}
while (edge.first != firstVtx);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment