diff --git a/src/common/imagtiff.cpp b/src/common/imagtiff.cpp index fed6679246..0df1139d8f 100644 --- a/src/common/imagtiff.cpp +++ b/src/common/imagtiff.cpp @@ -286,7 +286,6 @@ bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbos } uint32 w, h; - uint32 npixels; uint32 *raster; TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w ); @@ -300,9 +299,20 @@ bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbos (samplesInfo[0] == EXTRASAMPLE_ASSOCALPHA || samplesInfo[0] == EXTRASAMPLE_UNASSALPHA)); - npixels = w * h; + // guard against integer overflow during multiplication which could result + // in allocating a too small buffer and then overflowing it + const double bytesNeeded = w * h * sizeof(uint32); + if ( bytesNeeded >= wxUINT32_MAX ) + { + if ( verbose ) + wxLogError( _("TIFF: Image size is abnormally big.") ); - raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) ); + TIFFClose(tif); + + return false; + } + + raster = (uint32*) _TIFFmalloc( bytesNeeded ); if (!raster) {