summaryrefslogtreecommitdiff
path: root/libtiff/tif_lerc.c
diff options
context:
space:
mode:
authorLaramie Leavitt <lar@google.com>2022-09-16 04:11:48 +0000
committerEven Rouault <even.rouault@spatialys.com>2022-11-08 15:12:51 +0100
commitaa863b5e8206ab767c150a9941c8aa50ae9bb1d6 (patch)
tree2f8568d6dd98699124ae4e48c48d3aa36d751999 /libtiff/tif_lerc.c
parentddef030d3d19719f7902fed69f03d8a3bbd70bc8 (diff)
downloadlibtiff-git-aa863b5e8206ab767c150a9941c8aa50ae9bb1d6.tar.gz
Add reentrant error functions
Prior to this change, libtiff relied on global error handlers, which is problematic when libtiff used by multiple independent libraries from within the same process, as they may unwittingly clobber the error handling, introduce race conditions when setting handlers, or otherwise have unintended side effects. This change adds error handlers to the TIFF struct, which are used preferentially when available. The error handlers are invoked when the re-entrant error functions are called: void TIFFErrorExtR(TIFF*, const char* module, const char* fmt, ...) void TIFFWarningExtR(TIFF*, const char* module, const char* fmt, ...) The handlers have a similar signature to the existing extended handlers, additionally returning an int: int TIFFErrorHandlerExtR(thandle_t, const char*, const char*, va_list) thandle_t is the userdata passed to TIFFOpen When the handler returns 1, the global handlers are not called. Custom error/warning handlers may be installed on a per-file basis by calling the Set functions: TIFF* tif = TIFFOpen(...); TIFFSetErrorHandlerExtR(tif, MyErrorHandler); TIFFSetWarningHandlerExtR(tif, MyWarningHandler); Additionally, the callsites to TIFFErrorExt and TIFFWarningExt have been updated to call the reentrant versions.
Diffstat (limited to 'libtiff/tif_lerc.c')
-rw-r--r--libtiff/tif_lerc.c84
1 files changed, 42 insertions, 42 deletions
diff --git a/libtiff/tif_lerc.c b/libtiff/tif_lerc.c
index a4aeb4a1..9744fddc 100644
--- a/libtiff/tif_lerc.c
+++ b/libtiff/tif_lerc.c
@@ -168,7 +168,7 @@ static int GetLercDataType(TIFF* tif)
return 7;
}
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Unsupported combination of SampleFormat and td_bitspersample");
return -1;
}
@@ -218,7 +218,7 @@ static int SetupUncompressedBuffer(TIFF* tif, LERCState* sp,
new_alloc = (unsigned int)new_alloc_64;
if( new_alloc != new_alloc_64 )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Too large uncompressed strip/tile");
_TIFFfree(sp->uncompressed_buffer);
sp->uncompressed_buffer = 0;
@@ -232,7 +232,7 @@ static int SetupUncompressedBuffer(TIFF* tif, LERCState* sp,
sp->uncompressed_buffer = _TIFFmalloc(new_alloc);
if( !sp->uncompressed_buffer )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Cannot allocate buffer");
_TIFFfree(sp->uncompressed_buffer);
sp->uncompressed_buffer = 0;
@@ -257,7 +257,7 @@ static int SetupUncompressedBuffer(TIFF* tif, LERCState* sp,
void* mask_buffer = _TIFFrealloc(sp->mask_buffer, mask_size);
if( mask_buffer == NULL )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Cannot allocate buffer");
sp->mask_size = 0;
_TIFFfree(sp->uncompressed_buffer);
@@ -328,7 +328,7 @@ LERCPreDecode(TIFF* tif, uint16_t s)
sp->libdeflate_dec = libdeflate_alloc_decompressor();
if( sp->libdeflate_dec == NULL )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Cannot allocate decompressor");
return 0;
}
@@ -340,7 +340,7 @@ LERCPreDecode(TIFF* tif, uint16_t s)
&lerc_data_sizet);
if( res != LIBDEFLATE_SUCCESS )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Decoding error at scanline %lu",
(unsigned long) tif->tif_row);
return 0;
@@ -359,7 +359,7 @@ LERCPreDecode(TIFF* tif, uint16_t s)
zlib_ret = inflateInit(&strm);
if( zlib_ret != Z_OK )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"inflateInit() failed");
inflateEnd(&strm);
return 0;
@@ -372,7 +372,7 @@ LERCPreDecode(TIFF* tif, uint16_t s)
zlib_ret = inflate(&strm, Z_FINISH);
if( zlib_ret != Z_STREAM_END && zlib_ret != Z_OK )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"inflate() failed");
inflateEnd(&strm);
return 0;
@@ -392,7 +392,7 @@ LERCPreDecode(TIFF* tif, uint16_t s)
tif->tif_rawcp,
tif->tif_rawcc);
if( ZSTD_isError(zstd_ret) ) {
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Error in ZSTD_decompress(): %s",
ZSTD_getErrorName(zstd_ret));
return 0;
@@ -401,13 +401,13 @@ LERCPreDecode(TIFF* tif, uint16_t s)
lerc_data = sp->compressed_buffer;
lerc_data_size = (unsigned int)zstd_ret;
#else
- TIFFErrorExt(tif->tif_clientdata, module, "ZSTD support missing");
+ TIFFErrorExtR(tif, module, "ZSTD support missing");
return 0;
#endif
}
else if( sp->additional_compression != LERC_ADD_COMPRESSION_NONE )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Unhandled additional compression");
return 0;
}
@@ -421,7 +421,7 @@ LERCPreDecode(TIFF* tif, uint16_t s)
0);
if( lerc_ret != 0 )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"lerc_getBlobInfo() failed");
return 0;
}
@@ -453,48 +453,48 @@ LERCPreDecode(TIFF* tif, uint16_t s)
nRows, nBands, nValidPixels, blobSize } */
if( infoArray[0] != (unsigned)sp->lerc_version )
{
- TIFFWarningExt(tif->tif_clientdata, module,
+ TIFFWarningExtR(tif, module,
"Unexpected version number: %d. Expected: %d",
infoArray[0], sp->lerc_version);
}
if( infoArray[1] != (unsigned)lerc_data_type )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Unexpected dataType: %d. Expected: %d",
infoArray[1], lerc_data_type);
return 0;
}
if( infoArray[2] != (unsigned)ndims )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Unexpected nDim: %d. Expected: %d",
infoArray[2], ndims);
return 0;
}
if( infoArray[3] != sp->segment_width )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Unexpected nCols: %d. Expected: %du",
infoArray[3], sp->segment_width);
return 0;
}
if( infoArray[4] != sp->segment_height )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Unexpected nRows: %d. Expected: %u",
infoArray[4], sp->segment_height);
return 0;
}
if( infoArray[5] != 1 )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Unexpected nBands: %d. Expected: %d",
infoArray[5], 1);
return 0;
}
if( infoArray[7] != lerc_data_size )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Unexpected blobSize: %d. Expected: %u",
infoArray[7],
lerc_data_size);
@@ -516,7 +516,7 @@ LERCPreDecode(TIFF* tif, uint16_t s)
sp->uncompressed_buffer);
if( lerc_ret != 0 )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"lerc_decode() failed");
return 0;
}
@@ -601,7 +601,7 @@ LERCDecode(TIFF* tif, uint8_t* op, tmsize_t occ, uint16_t s)
if( sp->uncompressed_buffer == 0 )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Uncompressed buffer not allocated");
return 0;
}
@@ -609,7 +609,7 @@ LERCDecode(TIFF* tif, uint8_t* op, tmsize_t occ, uint16_t s)
if( (uint64_t)sp->uncompressed_offset +
(uint64_t)occ > sp->uncompressed_size )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Too many bytes read");
return 0;
}
@@ -678,7 +678,7 @@ LERCEncode(TIFF* tif, uint8_t* bp, tmsize_t cc, uint16_t s)
if( (uint64_t)sp->uncompressed_offset +
(uint64_t)cc > sp->uncompressed_size )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Too many bytes written");
return 0;
}
@@ -707,7 +707,7 @@ LERCPostEncode(TIFF* tif)
if( sp->uncompressed_offset != sp->uncompressed_size )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Unexpected number of bytes in the buffer");
return 0;
}
@@ -830,7 +830,7 @@ LERCPostEncode(TIFF* tif)
&numBytes);
if( lerc_ret != 0 )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"lerc_computeCompressedSize() failed");
return 0;
}
@@ -869,7 +869,7 @@ LERCPostEncode(TIFF* tif)
&numBytesWritten);
if( lerc_ret != 0 )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"lerc_encode() failed");
return 0;
}
@@ -888,7 +888,7 @@ LERCPostEncode(TIFF* tif)
sp->zipquality);
if( sp->libdeflate_enc == NULL )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Cannot allocate compressor");
return 0;
}
@@ -898,7 +898,7 @@ LERCPostEncode(TIFF* tif)
if( libdeflate_zlib_compress_bound(sp->libdeflate_enc, numBytesWritten) >
sp->uncompressed_alloc )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Output buffer for libdeflate too small");
return 0;
}
@@ -910,7 +910,7 @@ LERCPostEncode(TIFF* tif)
if( tif->tif_rawcc == 0 )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Encoder error at scanline %lu",
(unsigned long) tif->tif_row);
return 0;
@@ -929,7 +929,7 @@ LERCPostEncode(TIFF* tif)
zlib_ret = deflateInit(&strm, cappedQuality);
if( zlib_ret != Z_OK )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"deflateInit() failed");
return 0;
}
@@ -946,7 +946,7 @@ LERCPostEncode(TIFF* tif)
deflateEnd(&strm);
if( zlib_ret != Z_STREAM_END )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"deflate() failed");
return 0;
}
@@ -972,7 +972,7 @@ LERCPostEncode(TIFF* tif)
numBytesWritten,
sp->zstd_compress_level );
if( ZSTD_isError(zstd_ret) ) {
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Error in ZSTD_compress(): %s",
ZSTD_getErrorName(zstd_ret));
return 0;
@@ -991,13 +991,13 @@ LERCPostEncode(TIFF* tif)
}
}
#else
- TIFFErrorExt(tif->tif_clientdata, module, "ZSTD support missing");
+ TIFFErrorExtR(tif, module, "ZSTD support missing");
return 0;
#endif
}
else if( sp->additional_compression != LERC_ADD_COMPRESSION_NONE )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Unhandled additional compression");
return 0;
}
@@ -1087,7 +1087,7 @@ LERCVSetField(TIFF* tif, uint32_t tag, va_list ap)
int* params = va_arg(ap, int*);
if( count < 2 )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Invalid count for LercParameters: %u", count);
return 0;
}
@@ -1105,7 +1105,7 @@ LERCVSetField(TIFF* tif, uint32_t tag, va_list ap)
int version = va_arg(ap, int);
if( version != LERC_VERSION_2_4 )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Invalid value for LercVersion: %d", version);
return 0;
}
@@ -1122,7 +1122,7 @@ LERCVSetField(TIFF* tif, uint32_t tag, va_list ap)
#ifndef ZSTD_SUPPORT
if( additional_compression == LERC_ADD_COMPRESSION_ZSTD )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"LERC_ZSTD requested, but ZSTD not available");
return 0;
}
@@ -1131,7 +1131,7 @@ LERCVSetField(TIFF* tif, uint32_t tag, va_list ap)
additional_compression != LERC_ADD_COMPRESSION_DEFLATE &&
additional_compression != LERC_ADD_COMPRESSION_ZSTD )
{
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Invalid value for LercAdditionalCompression: %d",
additional_compression);
return 0;
@@ -1149,7 +1149,7 @@ LERCVSetField(TIFF* tif, uint32_t tag, va_list ap)
if( sp->zstd_compress_level <= 0 ||
sp->zstd_compress_level > ZSTD_maxCLevel() )
{
- TIFFWarningExt(tif->tif_clientdata, module,
+ TIFFWarningExtR(tif, module,
"ZSTD_LEVEL should be between 1 and %d",
ZSTD_maxCLevel());
}
@@ -1161,7 +1161,7 @@ LERCVSetField(TIFF* tif, uint32_t tag, va_list ap)
sp->zipquality = (int) va_arg(ap, int);
if( sp->zipquality < Z_DEFAULT_COMPRESSION ||
sp->zipquality > LIBDEFLATE_MAX_COMPRESSION_LEVEL ) {
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Invalid ZipQuality value. Should be in [-1,%d] range",
LIBDEFLATE_MAX_COMPRESSION_LEVEL);
return 0;
@@ -1222,7 +1222,7 @@ int TIFFInitLERC(TIFF* tif, int scheme)
* Merge codec-specific tag information.
*/
if (!_TIFFMergeFields(tif, LERCFields, TIFFArrayCount(LERCFields))) {
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"Merging LERC codec-specific tags failed");
return 0;
}
@@ -1270,7 +1270,7 @@ int TIFFInitLERC(TIFF* tif, int scheme)
return 1;
bad:
- TIFFErrorExt(tif->tif_clientdata, module,
+ TIFFErrorExtR(tif, module,
"No space for LERC state block");
return 0;
}