Segmentation effects crashing Slicer

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:

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:

  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:

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:

  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

idS_test 2147483344
idS_test 2147484552
1 Like