Page MenuHomePhabricator

Potential crash in statistics calculator
Closed, InvalidPublic

Description

I'm not sure how to reproduce this in MITK but we reproduced this crash in our fork.

Problem is in smart pointers in calculator's members and in thrown exception.

Calculator contains two smart pointers to images: m_Image and m_InternalImage that is actually points to some slice from m_Image.
m_InternalImage is initialized in ImageStatisticsCalculator::ComputeStatistics() (inside ExtractImageAndMask()) and is cleared at the end of this function.
But if there is an exception in ExtractImageAndMask() (for example, if we calculate statistics for planar figure that is outside of image's boundaries), m_InternalImage isn't cleared at the end and still points to some slice. So far so good.

Later, when you set up new image to calculator, old image from m_Image is deleted but its ImageData isn't because m_InternalImage data holds it as parent. And of course m_InternalImage still exists at this point.
Later, ExtractImageAndMask() is called, m_InternalImage is rewritten, old image from it is deleted, image data from old m_Image is also deleted and deletes its own ImageVtkWriteAccessor. Which tries to access old m_Image that was deleted some time ago. And there is a crash.

I've a fix for this problem that moves m_InternalImage from member to local variable, so it will be deleted even in case of exception.
I'll make a PR soon.

Event Timeline

kislinsk added a project: Pull Request.
kislinsk added a subscriber: kislinsk.

As we have completely rewritten the statistics components, I close this task as Invalid. Please feel free to re-open, if there is still a problem with the new implementation, thank you.