Uninitialized memory read by vtkCellDataToPointData for some PLY files.
tldr: run the attached test app, uninitialized memory is read, as proved by valgrind.
Reading of specific PLY files (noticeably exported from SolidWorks) returns uninitialized scalars when used in conjunction with vtkCellDataToPointData
.
A PLY file that possesses point-data scalars (colours per Vertex) as opposed to cell-data scalars (colours per Face) behaves as expected. However for PLY files of the latter format, such files will require for example the use of the following class vtkCellDataToPointData
in order to be read properly by any application that processes / renders meshes using vertices (not faces).
In employing vtkCellDataToPointData
the resulting point-data scalars contain random values that are out of the expected range (0 - 255).
Using valgrind, I found these random values are due to reading uninitialized memory created with this function, with this backtrace:
==14956== Conditional jump or move depends on uninitialised value(s)
==14956== at 0x10A83C: main (main.cpp:86)
==14956== Uninitialised value was created by a heap allocation
==14956== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==14956== by 0x6E33678: vtkBuffer<float>::Allocate(long long) (vtkBuffer.h:179)
==14956== by 0x74F92AF: vtkAOSDataArrayTemplate<float>::AllocateTuples(long long) (vtkAOSDataArrayTemplate.txx:453)
==14956== by 0x73069A0: vtkGenericDataArray<vtkAOSDataArrayTemplate<float>, float>::AllocateTuples(long long) (vtkGenericDataArray.h:351)
==14956== by 0x7304383: vtkGenericDataArray<vtkAOSDataArrayTemplate<float>, float>::Allocate(long long, long long) (vtkGenericDataArray.txx:387)
==14956== by 0x730474A: vtkGenericDataArray<vtkAOSDataArrayTemplate<float>, float>::SetNumberOfTuples(long long) (vtkGenericDataArray.txx:475)
==14956== by 0x50DE4C5: ArrayList::AddArrays(long long, vtkDataSetAttributes*, vtkDataSetAttributes*, double, int) (vtkArrayListTemplate.txx:142)
==14956== by 0x528689E: (anonymous namespace)::UnstructuredDataCD2PD<vtkCellLinks>::UnstructuredDataCD2PD(long long, vtkDataSetAttributes*, vtkPointData*, vtkCellLinks*) (vtkCellDataToPointData.cxx:71)
==14956== by 0x5283756: (anonymous namespace)::FastUnstructuredData(long long, vtkAbstractCellLinks*, vtkDataSetAttributes*, vtkPointData*) (vtkCellDataToPointData.cxx:111)
==14956== by 0x5285961: vtkCellDataToPointData::RequestDataForUnstructuredData(vtkInformation*, vtkInformationVector**, vtkInformationVector*) (vtkCellDataToPointData.cxx:586)
==14956== by 0x5284387: vtkCellDataToPointData::RequestData(vtkInformation*, vtkInformationVector**, vtkInformationVector*) (vtkCellDataToPointData.cxx:394)
==14956== by 0x6157EB1: vtkDataSetAlgorithm::ProcessRequest(vtkInformation*, vtkInformationVector**, vtkInformationVector*) (vtkDataSetAlgorithm.cxx:162)
Previous VTK versions were not problematic. A git bitsect revealed d24724da changed behaviour. In previous versions, vtkCellDataToPointData
returned VTK_UNSIGNED_CHAR
as the Data Type for the resulting point-data scalars, but versions after returned VTK_FLOAT
. Furthermore, vtkCellDataToPointData
is now being Optimized for example for vtkUnstructuredGrid / vtkPolyData
of which there's a call to AddArrays(...)
that defaults to promoting the data type of the array. This may explain the change from VTK_UNSIGNED_CHAR
to VTK_FLOAT
.
This raises couple of questions pertaining to the call to AddArrays(...)
in vtkCellDataToPointData
see struct UnstructuredDataCD2PD{ ... }
:
- Is it necessary to promote the type? (I don't quite understand the essence of doing so)
- When
AddArrays(...)
is called we fall into an inlined function invtkArrayListTemplate.txx
inline void ArrayList::AddArrays(vtkIdType numOutPts, vtkDataSetAttributes* inPD, vtkDataSetAttributes* outPD, double nullValue, vtkTypeBool promote)
where there exists a suspicious comment in the promotion branch// nasty side effect will replace current array in the same spot
.
I'm now stuck debugging this and would appreciate any help. @cory.quammen @dgobbi @seanm @will.schroeder