# Crash in vtkPolyData.GetCell

**URL:** <https://discourse.vtk.org/t/crash-in-vtkpolydata-getcell/3731>\
**Category:** Development\
**Created:** [July 8, 2020, 5:50pm UTC](https://discourse.vtk.org/t/crash-in-vtkpolydata-getcell/3731 "2020-07-08T17:50:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![efahl](https://discourse.vtk.org/user_avatar/discourse.vtk.org/efahl/32/1087_2.png) [@efahl](https://discourse.vtk.org/u/efahl)\
**Post date:** [July 8, 2020, 5:50pm UTC](https://discourse.vtk.org/t/crash-in-vtkpolydata-getcell/3731/1 "2020-07-08T17:50:40Z")

</div>

I’m getting a seg fault when trying to extract a cut boundary…I think. I’m using Visual Studio and Python 3.8, which don’t work together yet, so I can see the cxx code at the point of failure, but have no idea how I got here.

The culprit code is in Common/DataModel/vtkPolyData.cxx, current head, lines 149…

```auto
vtkCell* vtkPolyData::GetCell(vtkIdType cellId)
{
  if (!this->Cells)
  {
    this->BuildCells();
  }

  const TaggedCellId tag = this->Cells->GetTag(cellId);

```

The polydata has four polys, apparently all degenerate, so down in BuildCells, it throws at line 932, catches at line 958, sets ‘this-\>Cells = nullptr’ and returns. Seg fault occurs immediately on the last line quoted above (and I see the same and similar patterns elsewhere in this file).

Should BuildCells be returning an ‘EmptyCells’, or should the GetCell code check for null and set ‘tag’ to some none-ish value, or ???

I’m trying to track down the root cause (why and where we’re producing a degenerate poly), but it seems like GetCell and friends should not be seg faulting like this.

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [July 8, 2020, 6:00pm UTC](https://discourse.vtk.org/t/crash-in-vtkpolydata-getcell/3731/2 "2020-07-08T18:00:05Z")

</div>

Can you check if this has been already fixed in latest VTK master?

---

<div class="post-metadata">

**Author:** ![efahl](https://discourse.vtk.org/user_avatar/discourse.vtk.org/efahl/32/1087_2.png) [@efahl](https://discourse.vtk.org/u/efahl)\
**Post date:** [July 8, 2020, 6:13pm UTC](https://discourse.vtk.org/t/crash-in-vtkpolydata-getcell/3731/3 "2020-07-08T18:13:11Z")

</div>

Hi Andras,

The above code snippet was cut from latest on github committed May 6, is that the master branch? (Or is it on gitlab or somewhere else?)

Eric

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [July 8, 2020, 6:19pm UTC](https://discourse.vtk.org/t/crash-in-vtkpolydata-getcell/3731/4 "2020-07-08T18:19:17Z")

</div>

OK, if it is not fixed there then it would be a great help if you could submit a merge request with a test that reproduces the issue and a proposed fix. Details of of how to create merge request are described here: [https://github.com/Kitware/VTK/blob/master/CONTRIBUTING.md](https://github.com/Kitware/VTK/blob/master/CONTRIBUTING.md).

---

<div class="post-metadata">

**Author:** ![AllisonVacanti](https://discourse.vtk.org/user_avatar/discourse.vtk.org/allisonvacanti/32/2396_2.png) [@AllisonVacanti](https://discourse.vtk.org/u/AllisonVacanti)\
**Post date:** [July 12, 2020, 4:32pm UTC](https://discourse.vtk.org/t/crash-in-vtkpolydata-getcell/3731/5 "2020-07-12T16:32:00Z")

</div>

There should be a message in the error log explaining why BuildCells failed. The line numbers seem to have changed slightly in the current master, but I think you’re hitting this condition:

[https://gitlab.kitware.com/vtk/vtk/blob/master/Common/DataModel/vtkPolyData.cxx#L932-935](https://gitlab.kitware.com/vtk/vtk/blob/master/Common/DataModel/vtkPolyData.cxx#L932-935)

```auto
        if (size < 3)
        {
          throw std::runtime_error("Invalid cell size for polys.");
        }

```

This checks that all cells in the `Polys` cell array are defined with at least three points. The `Polys` cell array must only contain `VTK_TRIANGLE`, `VTK_QUAD`, or `VTK_POLYGON` cells, all of which require at least 3 points to be valid.

Can you verify that your polydata is correctly defined?

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [July 12, 2020, 6:18pm UTC](https://discourse.vtk.org/t/crash-in-vtkpolydata-getcell/3731/6 "2020-07-12T18:18:23Z")

</div>

Is VTK now allowed to throw exceptions on its public interface?

---

<div class="post-metadata">

**Author:** ![AllisonVacanti](https://discourse.vtk.org/user_avatar/discourse.vtk.org/allisonvacanti/32/2396_2.png) [@AllisonVacanti](https://discourse.vtk.org/u/AllisonVacanti)\
**Post date:** [July 12, 2020, 6:37pm UTC](https://discourse.vtk.org/t/crash-in-vtkpolydata-getcell/3731/7 "2020-07-12T18:37:54Z")

</div>

I’m not sure if there’s a policy for that. We use them internally, but I can’t think of a place where they’re used in API.

These particular exceptions are just implementation details, and are always caught and handled before they would escape an API boundary by logging an error and discarding the exception:

[https://gitlab.kitware.com/vtk/vtk/-/blob/master/Common/DataModel/vtkPolyData.cxx#L905-964](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Common/DataModel/vtkPolyData.cxx#L905-964)

---

<div class="post-metadata">

**Author:** ![efahl](https://discourse.vtk.org/user_avatar/discourse.vtk.org/efahl/32/1087_2.png) [@efahl](https://discourse.vtk.org/u/efahl)\
**Post date:** [July 12, 2020, 6:58pm UTC](https://discourse.vtk.org/t/crash-in-vtkpolydata-getcell/3731/8 "2020-07-12T18:58:53Z")

</div>

I came up with a reproducer this morning, it’s as you suspect, an invalid polygon:

```auto
#!/usr/bin/env python

import vtk
print(vtk.vtkVersion().GetVTKSourceVersion())

points = vtk.vtkPoints()
points.InsertNextPoint([1, 2, 3])
points.InsertNextPoint([4, 5, 6])

polys = vtk.vtkCellArray()
polys.InsertNextCell(2, [0, 1]) # Degenerate polygon.

polydata = vtk.vtkPolyData()
polydata.SetPoints(points)
polydata.SetPolys (polys)

cell = polydata.GetCell(0)

```

This causes a crash in 8.90 and 9.0, but doesn’t segfault in 8.2.

```auto
> ./degen_poly.py
9.0.0
2020-07-12 11:58:13.054 ( 0.245s) [] vtkPolyData.cxx:961 ERR| vtkPolyData (000001DDC46B8950): Error while constructing cell map: Invalid cell size for polys.
Segmentation fault

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [July 12, 2020, 7:00pm UTC](https://discourse.vtk.org/t/crash-in-vtkpolydata-getcell/3731/9 "2020-07-12T19:00:03Z")

</div>

> [@AllisonVacanti](#):
>
> These particular exceptions are just implementation details, and are always caught and handled

OK, internal use is no problem at all. Of course it would cause huge issues in VTK and in applications if uncaught exceptions leaked out.
