@@ -171,7 +171,7 @@ int ImagingLibTiffInit(ImagingCodecState state, int fp, uint32 offset) {
171171
172172
173173int ReadTile (TIFF * tiff , UINT32 col , UINT32 row , UINT32 * buffer ) {
174- uint16 photometric ;
174+ uint16 photometric = 0 ;
175175
176176 TIFFGetField (tiff , TIFFTAG_PHOTOMETRIC , & photometric );
177177
@@ -228,7 +228,7 @@ int ReadTile(TIFF* tiff, UINT32 col, UINT32 row, UINT32* buffer) {
228228}
229229
230230int ReadStrip (TIFF * tiff , UINT32 row , UINT32 * buffer ) {
231- uint16 photometric ;
231+ uint16 photometric = 0 ; // init to not PHOTOMETRIC_YCBCR
232232 TIFFGetField (tiff , TIFFTAG_PHOTOMETRIC , & photometric );
233233
234234 // To avoid dealing with YCbCr subsampling, let libtiff handle it
@@ -363,6 +363,13 @@ int ImagingLibTiffDecode(Imaging im, ImagingCodecState state, UINT8* buffer, Py_
363363
364364 state -> bytes = row_byte_size * tile_length ;
365365
366+ if (TIFFTileSize (tiff ) > state -> bytes ) {
367+ // If the strip size as expected by LibTiff isn't what we're expecting, abort.
368+ state -> errcode = IMAGING_CODEC_MEMORY ;
369+ TIFFClose (tiff );
370+ return -1 ;
371+ }
372+
366373 /* realloc to fit whole tile */
367374 /* malloc check above */
368375 new_data = realloc (state -> buffer , state -> bytes );
@@ -424,11 +431,21 @@ int ImagingLibTiffDecode(Imaging im, ImagingCodecState state, UINT8* buffer, Py_
424431 TIFFClose (tiff );
425432 return -1 ;
426433 }
427-
434+
428435 state -> bytes = rows_per_strip * row_byte_size ;
429436
430437 TRACE (("StripSize: %d \n" , state -> bytes ));
431438
439+ if (TIFFStripSize (tiff ) > state -> bytes ) {
440+ // If the strip size as expected by LibTiff isn't what we're expecting, abort.
441+ // man: TIFFStripSize returns the equivalent size for a strip of data as it would be returned in a
442+ // call to TIFFReadEncodedStrip ...
443+
444+ state -> errcode = IMAGING_CODEC_MEMORY ;
445+ TIFFClose (tiff );
446+ return -1 ;
447+ }
448+
432449 /* realloc to fit whole strip */
433450 /* malloc check above */
434451 new_data = realloc (state -> buffer , state -> bytes );
0 commit comments