Merge branch 'jpeg_multiscan_dos_logic' into 'master'

tif_jpeg.c: revise logic to detect potential excessive memory usage when...

See merge request libtiff/libtiff!147
This commit is contained in:
Even Rouault 2020-04-25 10:25:43 +00:00
commit b683c37524

View File

@ -1206,35 +1206,34 @@ JPEGPreDecode(TIFF* tif, uint16 s)
/* store for all coefficients */ /* store for all coefficients */
/* See call to jinit_d_coef_controller() from master_selection() */ /* See call to jinit_d_coef_controller() from master_selection() */
/* in libjpeg */ /* in libjpeg */
toff_t nRequiredMemory = (toff_t)sp->cinfo.d.image_width *
sp->cinfo.d.image_height *
sp->cinfo.d.num_components *
((td->td_bitspersample+7)/8);
/* BLOCK_SMOOTHING_SUPPORTED is generally defined, so we need */
/* to replicate the logic of jinit_d_coef_controller() */
if( sp->cinfo.d.progressive_mode )
nRequiredMemory *= 3;
#ifndef TIFF_LIBJPEG_LARGEST_MEM_ALLOC /* 1 MB for regular libjpeg usage */
#define TIFF_LIBJPEG_LARGEST_MEM_ALLOC (100 * 1024 * 1024) toff_t nRequiredMemory = 1024 * 1024;
#endif
if( nRequiredMemory > TIFF_LIBJPEG_LARGEST_MEM_ALLOC && for (ci = 0; ci < sp->cinfo.d.num_components; ci++) {
const jpeg_component_info *compptr = &(sp->cinfo.d.comp_info[ci]);
nRequiredMemory += (toff_t)(
((compptr->width_in_blocks + compptr->h_samp_factor - 1) / compptr->h_samp_factor)) *
((compptr->height_in_blocks + compptr->v_samp_factor - 1) / compptr->v_samp_factor) *
sizeof(JBLOCK);
}
if( sp->cinfo.d.mem->max_memory_to_use > 0 &&
nRequiredMemory > (toff_t)(sp->cinfo.d.mem->max_memory_to_use) &&
getenv("LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC") == NULL ) getenv("LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC") == NULL )
{ {
TIFFErrorExt(tif->tif_clientdata, module, TIFFErrorExt(tif->tif_clientdata, module,
"Reading this strip would require libjpeg to allocate " "Reading this image would require libjpeg to allocate "
"at least %u bytes. " "at least %u bytes. "
"This is disabled since above the %u threshold. " "This is disabled since above the %u threshold. "
"You may override this restriction by defining the " "You may override this restriction by defining the "
"LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC environment variable, " "LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC environment variable, "
"or recompile libtiff by defining the " "or setting the JPEGMEM environment variable to a value greater "
"TIFF_LIBJPEG_LARGEST_MEM_ALLOC macro to a value greater " "or equal to '%uM'",
"than %u", (unsigned)(nRequiredMemory),
(unsigned)nRequiredMemory, (unsigned)(sp->cinfo.d.mem->max_memory_to_use),
(unsigned)TIFF_LIBJPEG_LARGEST_MEM_ALLOC, (unsigned)((nRequiredMemory + 1000000 - 1) / 1000000));
(unsigned)TIFF_LIBJPEG_LARGEST_MEM_ALLOC); return 0;
return (0);
} }
} }