# Segmentation effects crashing Slicer

**URL:** <https://discourse.slicer.org/t/segmentation-effects-crashing-slicer/7474>\
**Category:** Support\
**Created:** [July 8, 2019, 10:26pm UTC](https://discourse.slicer.org/t/segmentation-effects-crashing-slicer/7474 "2019-07-08T22:26:01Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [July 30, 2019, 10:27pm UTC](https://discourse.slicer.org/t/segmentation-effects-crashing-slicer/7474/4 "2019-07-30T22:27:01Z")

</div>

After trying to load the dataset, I didn’t get a bad allocation. Instead I got a crash due to a lookup in scalar array using a negative index.

Code where the error occur is the following:

> <https://github.com/Kitware/VTK/blob/2cd7e84cb92a5bc091e2764bdfd35a01caafc47c/Imaging/Stencil/vtkImageToImageStencil.cxx#L146-L154>

The crash occur when executing the line `inScalars->GetComponent(idS++,0);` with `idS = -2147482744`

It turns out that the `idS` ivar is of type `int`, and the value overflow … it should be changed to `vtkIdType` along with all intermediate variables.

The following snippet allows to reproduce the incorrect index value:

```auto
  int extent_0 = 0;
  int extent_1 = 1207;
  int extent_2 = 0;
  int extent_3 = 1250;
  int extent_4 = 0;
  int idY = 47;
  int idZ = 1421;

  int idS_test = ((extent_1 - extent_0 + 1)*
                   ((extent_3 - extent_2 + 1)*(idZ - extent_4) +
                    (idY - extent_2)));
  std::cout << "idS_test " << idS_test << std::endl;

  idY = 48;

  idS_test = ((extent_1 - extent_0 + 1)*
                     ((extent_3 - extent_2 + 1)*(idZ - extent_4) +
                      (idY - extent_2)));
  std::cout << "idS_test " << idS_test << std::endl;

```

associated output is:

```auto
idS_test 2147483344
idS_test -2147482744

```

### Proposed path forward

To move forward,

We need to change the type of all intermediate variable to `vtkIdType`. The following work as expected:

```auto
  vtkIdType extent_0 = 0;
  vtkIdType extent_1 = 1207;
  vtkIdType extent_2 = 0;
  vtkIdType extent_3 = 1250;
  vtkIdType extent_4 = 0;
  vtkIdType idY = 47;
  vtkIdType idZ = 1421;

  vtkIdType idS_test = ((extent_1 - extent_0 + 1)*
                   ((extent_3 - extent_2 + 1)*(idZ - extent_4) +
                    (idY - extent_2)));
  std::cout << "idS_test " << idS_test << std::endl;

  idY = 48;

  idS_test = ((extent_1 - extent_0 + 1)*
                     ((extent_3 - extent_2 + 1)*(idZ - extent_4) +
                      (idY - extent_2)));
  std::cout << "idS_test " << idS_test << std::endl;

```

Output

```auto
idS_test 2147483344
idS_test 2147484552

```

---

_[View the full topic](https://discourse.slicer.org/t/segmentation-effects-crashing-slicer/7474)._
