Skip to content

Commit 14825ce

Browse files
pierreldffmalaterre
authored andcommitted
Fix bug #512: Crash when reading corrupted Jpeg2000 files
Prevent overlay extraction in case of malformed overlay or image information. Add warning to prevent user
1 parent 7ce970f commit 14825ce

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

Source/MediaStorageAndFileFormat/gdcmJPEG2000Codec.cxx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,8 @@ bool JPEG2000Codec::GetHeaderInfo(const char * dummy_buffer, size_t buf_size, Tr
14231423
opj_stream_t *cio = nullptr;
14241424
opj_image_t *image = nullptr;
14251425
const unsigned char *src = (const unsigned char*)dummy_buffer;
1426+
if(!src)
1427+
return false ;
14261428
size_t file_length = buf_size;
14271429

14281430
/* set decoding parameters to default values */

Source/MediaStorageAndFileFormat/gdcmOverlay.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ class GDCM_EXPORT Overlay : public Object
9393

9494
/// set overlay from byte array + length
9595
void SetOverlay(const char *array, size_t length);
96-
///
96+
97+
/// \warning Before calling this method, you must verify the consistency
98+
/// between the image metadata (Image PixelFormat, Rows, Columns) and the
99+
/// overlay parameters. This pre-verification is required to ensure that the
100+
/// bit-depth is compatible and that the overlay data fits within the
101+
/// allocated pixel storage.
97102
bool GrabOverlayFromPixelData(DataSet const &ds);
98103

99104
/// Return the Overlay Data as ByteValue:

Source/MediaStorageAndFileFormat/gdcmPixmapReader.cxx

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,44 @@ static bool DoOverlays(const DataSet& ds, Pixmap& pixeldata)
546546
{
547547
gdcmWarningMacro( "Bits Allocated are wrong. Correcting." );
548548
ov.SetBitsAllocated( pixeldata.GetPixelFormat().GetBitsAllocated() );
549-
}
549+
}
550+
const DataElement &pixeldataDe = ds.GetDataElement(Tag(0x7fe0, 0x0010));
551+
const ByteValue *bv = pixeldataDe.GetByteValue();
552+
if (!bv) {
553+
gdcmWarningMacro(
554+
"Could not extract overlay from encapsulated stream.");
555+
continue;
556+
}
557+
unsigned long computedFramePixelsNb =
558+
pixeldata.GetDimension(0) * pixeldata.GetDimension(1);
559+
560+
if (pixeldata.GetPixelFormat().GetPixelSize() == 0 ||
561+
computedFramePixelsNb >
562+
bv->GetLength() / pixeldata.GetPixelFormat().GetPixelSize()) {
563+
gdcmWarningMacro("Image information is not persistent. Can't extract overlay #"
564+
<< idxoverlays);
565+
continue;
566+
}
567+
signed short ovOriginY = ov.GetOrigin()[0];
568+
signed short ovOriginX = ov.GetOrigin()[1];
569+
long startPixel =
570+
(ovOriginX - 1) + (ovOriginY - 1) * pixeldata.GetDimension(0);
571+
if (startPixel < 0 ||
572+
(unsigned long)startPixel >= computedFramePixelsNb) {
573+
gdcmWarningMacro(
574+
"Origin is not in image buffer. Can't extract overlay #"
575+
<< idxoverlays);
576+
continue;
577+
}
578+
unsigned long lastPixelAccessed =
579+
(unsigned long)startPixel +
580+
(ov.GetRows() - 1) * pixeldata.GetDimension(0) +
581+
(ov.GetColumns() - 1);
582+
if (lastPixelAccessed >= computedFramePixelsNb) {
583+
gdcmWarningMacro("Overlay not fit image buffer. Can't extract overlay "
584+
<< idxoverlays);
585+
continue;
586+
}
550587

551588
if( !ov.GrabOverlayFromPixelData(ds) )
552589
{

0 commit comments

Comments
 (0)