From fea55b4fbe6a6ccfc212af4c87a6af2a5420751b Mon Sep 17 00:00:00 2001 From: Andras Lasso Date: Mon, 18 Mar 2024 23:03:51 -0400 Subject: [PATCH] BUG: Fix GDCM crash when reading DICOM image GDCM crashed when a DICOM image that had 32 bits allocated. The problem was that DoOverlayCleanup returned failure but that return value was ignored and later an empty buffer read was attempted. This fix avoids the crash by not ignoring the failed overlay cleanup. Applications can detect the error and switch to DCMTK IO to read such files (DCMTK can correctly read files with 32 bits allocated). --- Source/MediaStorageAndFileFormat/gdcmImageCodec.cxx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Source/MediaStorageAndFileFormat/gdcmImageCodec.cxx b/Source/MediaStorageAndFileFormat/gdcmImageCodec.cxx index c2030c0a8..2c25a90f9 100644 --- a/Source/MediaStorageAndFileFormat/gdcmImageCodec.cxx +++ b/Source/MediaStorageAndFileFormat/gdcmImageCodec.cxx @@ -675,6 +675,7 @@ bool ImageCodec::DecodeByStreams(std::istream &is, std::ostream &os) // Do the overlay cleanup (cleanup the unused bits) // must be the last operation (duh!) + bool copySuccess = false; if ( PF.GetBitsAllocated() != PF.GetBitsStored() && PF.GetBitsAllocated() != 8 ) { @@ -685,21 +686,23 @@ bool ImageCodec::DecodeByStreams(std::istream &is, std::ostream &os) // Sigh, I finally found someone not declaring that unused bits where not zero: // gdcmConformanceTests/dcm4chee_unusedbits_not_zero.dcm if( NeedOverlayCleanup ) - DoOverlayCleanup(*cur_is,os); + { + copySuccess = DoOverlayCleanup(*cur_is, os); + } else { // Once the issue with IMAGES/JPLY/RG3_JPLY aka gdcmData/D_CLUNIE_RG3_JPLY.dcm is solved the previous // code will be replace with a simple call to: - DoSimpleCopy(*cur_is,os); + copySuccess = DoSimpleCopy(*cur_is, os); } } else { assert( PF.GetBitsAllocated() == PF.GetBitsStored() ); - DoSimpleCopy(*cur_is,os); + copySuccess = DoSimpleCopy(*cur_is, os); } - return true; + return copySuccess; } bool ImageCodec::IsValid(PhotometricInterpretation const &)