Skip to content

Fix crash in vtkPointLocator when number of points is changed to zero

Sunderlandkyl requested to merge Sunderlandkyl/vtk:vtkPointLocator_crash into master

When the number of points in the input dataset was set to zero, BuildLocator would be called but the HashTable would not be invalidated. The FindClosestPoint methods would continue on with the invalid HashTable, resulting in a crash when invalid point IDs were accessed.

  • Fixed by calling FreeSearchStructure before returning from BuildLocatorInternal so that the HashTable is invalidated.
  • FreeSearchStructure now also re-initializes variables that are calculated during BuildLocatorInternal so that Bounds/H/Divisions will be consistent for an empty dataset.
  • Removed vtkErrorMacro when the dataset is empty or has no points since it is a valid input.

Example Python code to replicate the crash:

points = vtk.vtkPoints()
polyData = vtk.vtkPolyData()
polyData.SetPoints(points)
points.SetNumberOfPoints(10)
for i in range(10):
  points.SetPoint(i,i,i,i)
points.Modified()

locator = vtk.vtkPointLocator()
locator.SetDataSet(polyData)

print(locator.FindClosestPoint(0,0,0)) # Ok. Expected value 0
points.SetNumberOfPoints(0)
points.Modified()
print(locator.FindClosestPoint(0,0,0)) # Crash. Expected value -1

Merge request reports