From 63590b85bbe5016ecb863a27007ce22979c27bc9 Mon Sep 17 00:00:00 2001 From: Hans Johnson Date: Wed, 3 Aug 2022 11:21:40 -0500 Subject: [PATCH] BUG: Provide itk::Exception for invalid NIFTI write When attempting to write a nifti file to an invalid location (for example, C:\Windows\something.nii) then no file is created there yet no error is reported. The issue is that the error is swallowed in both niftilib and nifti2 (just "reported" with LNI_FERR macro, which does not throw an exception or reports the error to ITK in any other way): Resolves: #1958 --- Modules/IO/NIFTI/src/itkNiftiImageIO.cxx | 20 +++++++++++++++---- .../IO/NIFTI/test/itkNiftiImageIOTest5.cxx | 10 ++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/Modules/IO/NIFTI/src/itkNiftiImageIO.cxx b/Modules/IO/NIFTI/src/itkNiftiImageIO.cxx index a9dc336d4e1..9b98ae39dd1 100644 --- a/Modules/IO/NIFTI/src/itkNiftiImageIO.cxx +++ b/Modules/IO/NIFTI/src/itkNiftiImageIO.cxx @@ -2221,9 +2221,16 @@ NiftiImageIO ::Write(const void * buffer) // Need a const cast here so that we don't have to copy the memory // for writing. this->m_NiftiImage->data = const_cast(buffer); - nifti_image_write(this->m_NiftiImage); - this->m_NiftiImage->data = nullptr; // if left pointing to data buffer - // nifti_image_free will try and free this memory + const int nifti_write_status = nifti_image_write_status(this->m_NiftiImage); + this->m_NiftiImage->data = nullptr; // Must free before throwing exception. + // if left pointing to data buffer + // nifti_image_free inside Destructor of ITKNiftiIO + // will try and free this memory, and then + // so will destructor of the image that really owns it. + if (nifti_write_status) + { + itkExceptionMacro(<< "ERROR: nifti library failed to write image" << this->GetFileName()); + } } else /// Image intent is vector image { @@ -2301,8 +2308,13 @@ NiftiImageIO ::Write(const void * buffer) // Need a const cast here so that we don't have to copy the memory for // writing. this->m_NiftiImage->data = static_cast(nifti_buf); - nifti_image_write(this->m_NiftiImage); + const int nifti_write_status = nifti_image_write_status(this->m_NiftiImage); this->m_NiftiImage->data = nullptr; // if left pointing to data buffer + if (nifti_write_status) + { + itkExceptionMacro(<< "ERROR: nifti library failed to write image" << this->GetFileName()); + } + delete[] nifti_buf; } } diff --git a/Modules/IO/NIFTI/test/itkNiftiImageIOTest5.cxx b/Modules/IO/NIFTI/test/itkNiftiImageIOTest5.cxx index 8cb2383da29..5e74f3eabd2 100644 --- a/Modules/IO/NIFTI/test/itkNiftiImageIOTest5.cxx +++ b/Modules/IO/NIFTI/test/itkNiftiImageIOTest5.cxx @@ -87,8 +87,14 @@ SlopeInterceptTest() { static_cast(niftiImage->data)[i] = i; } - nifti_image_write(niftiImage); - nifti_image_free(niftiImage); + + const int nifti_write_status = nifti_image_write_status(niftiImage); + nifti_image_free(niftiImage); // Must free before throwing exception. + if (nifti_write_status) + { + itkGenericExceptionMacro(<< "ERROR: nifti library failed to write image" << filename); + } + // // read the image back in using ImageType = typename itk::Image;