Help with null deref in 'itkTreeContainerTest'

Hi all,

With UBSan, one of my dashboard bots finds a null deref in ‘itkTreeContainerTest’:

/ITK/Modules/Core/Common/include/itkPreOrderTreeIterator.h:99:28: runtime error: member call on null pointer of type ‘itk::TreeNode’

Indeed, if you add an “assert(this->m_Position)” you will see this bug even without UBSan.

The correct fix is not obvious to me. Is it even correct to invoke Next() if there is no next? If so, what should be returned? If not, then what should the semantics of operator++ be?

The crash backtrace is:

frame #4: 0x0000000101447e1c ITKCommon2TestDriver`itk::PreOrderTreeIterator<itk::TreeContainer<int> >::Next(this=0x00007fff5fbfb308) at itkPreOrderTreeIterator.h:101
frame #5: 0x00000001013c8dbd ITKCommon2TestDriver`itk::TreeIteratorBase<itk::TreeContainer<int> >::operator++(this=0x00007fff5fbfb308) at itkTreeIteratorBase.h:168
frame #6: 0x00000001013cf8b5 ITKCommon2TestDriver`itk::TreeContainer<int>::Count(this=0x0000000109a2d0a0) const at itkTreeContainer.hxx:92
frame #7: 0x00000001013cdac2 ITKCommon2TestDriver`itk::TreeContainer<int>::PrintSelf(this=0x0000000109a2d0a0, os=0x00007fff94883660, indent=(m_Indent = 2)) const at itkTreeContainer.hxx:330
frame #8: 0x0000000102074f5b ITKCommon2TestDriver`itk::LightObject::Print(this=0x0000000109a2d0a0, os=0x00007fff94883660, indent=(m_Indent = 0)) const at itkLightObject.cxx:130
frame #9: 0x00000001013ebbfd ITKCommon2TestDriver`itk::SmartPointer<itk::TreeContainer<int> >::Print(this=0x00007fff5fbfd850, os=0x00007fff94883660) const at itkSmartPointer.h:166
frame #10: 0x00000001013c7c24 ITKCommon2TestDriver`std::__1::basic_ostream<char, std::__1::char_traits<char> >& itk::operator<<<itk::TreeContainer<int> >(os=0x00007fff94883660, p=SmartPointer<itk::TreeContainer<int> > @ 0x00007fff5fbfd850) at itkSmartPointer.h:250
frame #11: 0x00000001013b4d78 ITKCommon2TestDriver`itkTreeContainerTest((null)=1, (null)=0x00007fff5fbff858) at itkTreeContainerTest.cxx:49
frame #12: 0x00000001000bcf02 ITKCommon2TestDriver`main(ac=1, av=0x00007fff5fbff858) at ITKCommon2TestDriver.cxx:521

Thanks,

Sean

I can reproduce this using Visual Studio. Looking into it.

Patch submitted.