Merge branch 'fix-goto-variable-init' into 'master'

Fix goto bypassing variable initialisation

See merge request libtiff/libtiff!783
This commit is contained in:
Roger Leigh
2025-12-14 12:12:57 +00:00
5 changed files with 50 additions and 28 deletions

View File

@@ -308,6 +308,7 @@ TIFF *TIFFClientOpenExt(const char *name, const char *mode,
TIFF *tif;
int m;
const char *cp;
tmsize_t size_to_alloc;
/* The following are configuration checks. They should be redundant, but
* should not compile to any actual code in an optimised release build
@@ -340,7 +341,7 @@ TIFF *TIFFClientOpenExt(const char *name, const char *mode,
m = _TIFFgetMode(opts, clientdata, mode, module);
if (m == -1)
goto bad2;
tmsize_t size_to_alloc = (tmsize_t)(sizeof(TIFF) + strlen(name) + 1);
size_to_alloc = (tmsize_t)(sizeof(TIFF) + strlen(name) + 1);
if (opts && opts->max_single_mem_alloc > 0 &&
size_to_alloc > opts->max_single_mem_alloc)
{

View File

@@ -326,12 +326,18 @@ int test_IFD_enlargement(const char *filename, unsigned int openMode,
uint64_t offsetBase[NUMIFDsMAX] = {0};
uint64_t offsetNew[NUMIFDsMAX];
unsigned char bufLine[NUMIFDsMAX + 1][1024];
TIFF *tif;
char xmldata[20];
uint32_t xmlpacketLength;
char xmldata0[] = "XMLb";
char xmldata1[] = "XML data package";
char xmldata2[] = "XML data package even longer";
assert(numIFDs <= NUMIFDsMAX);
assert(openMode < (sizeof(modeStrings) / sizeof(modeStrings[0])));
/*-- Create a file and write numIFDs IFDs directly to it --*/
TIFF *tif = TIFFOpen(filename, modeStrings[openMode]);
tif = TIFFOpen(filename, modeStrings[openMode]);
if (!tif)
{
fprintf(stderr, "Can't create %s. Testline %d\n", filename, __LINE__);
@@ -354,12 +360,11 @@ int test_IFD_enlargement(const char *filename, unsigned int openMode,
/* xmldata need to be 9 bytes (17 bytes for BigTIFF, respectively) thus
* not to fit into the entry itself and have space to cover two
* strip-/tile-offsets when XMLPACKET tag contents is reduced. */
char xmldata[20];
if (TIFFIsBigTIFF(tif))
strcpy(xmldata, "XML-ABCDEABCDEFGH");
else
strcpy(xmldata, "XML-ABCDE");
uint32_t xmlpacketLength = (uint32_t)strlen(xmldata);
xmlpacketLength = (uint32_t)strlen(xmldata);
if (!TIFFSetField(tif, TIFFTAG_XMLPACKET, xmlpacketLength, xmldata))
{
@@ -466,8 +471,7 @@ int test_IFD_enlargement(const char *filename, unsigned int openMode,
* This leaves some bytes of the larger previous IFD at EOF or before the
* next IFD. */
TIFFSetDirectory(tif, 0);
char xmldata0[] = "XMLb";
uint32_t xmlpacketLength = (uint32_t)strlen(xmldata0);
xmlpacketLength = (uint32_t)strlen(xmldata0);
if (!TIFFSetField(tif, TIFFTAG_XMLPACKET, xmlpacketLength, xmldata0))
{
@@ -555,7 +559,6 @@ int test_IFD_enlargement(const char *filename, unsigned int openMode,
}
/*-- Enlarge IFD 0 with enlarged tag data and write it again. --*/
char xmldata1[] = "XML data package";
xmlpacketLength = (uint32_t)strlen(xmldata1);
if (!TIFFSetField(tif, TIFFTAG_XMLPACKET, xmlpacketLength, xmldata1))
@@ -751,7 +754,6 @@ int test_IFD_enlargement(const char *filename, unsigned int openMode,
offsetNew[0], offsetNew[0], filename, __LINE__);
goto failure;
}
char xmldata2[] = "XML data package even longer";
if (!TIFFSetField(tif, TIFFTAG_XMLPACKET, (uint32_t)strlen(xmldata2),
xmldata2))
{
@@ -1194,10 +1196,12 @@ int test_SubIFD_enlargement(const char *filename, bool is_big_tiff)
TIFFErrorHandler errHandler = NULL;
uint64_t offsetBase[NUMBER_OF_DIRS];
TIFF *tif;
tdir_t numberOfMainIFDs;
/*-- Create a file and write NUMBER_OF_DIRS IFDs with
* NUMBER_OF_SUBIFDs SubIFDs to this file. --*/
TIFF *tif = TIFFOpen(filename, is_big_tiff ? "w8" : "w");
tif = TIFFOpen(filename, is_big_tiff ? "w8" : "w");
if (!tif)
{
fprintf(stderr, "Can't create %s. Testline %d\n", filename, __LINE__);
@@ -1261,7 +1265,7 @@ int test_SubIFD_enlargement(const char *filename, bool is_big_tiff)
goto failure;
}
tdir_t numberOfMainIFDs = TIFFNumberOfDirectories(tif);
numberOfMainIFDs = TIFFNumberOfDirectories(tif);
if (numberOfMainIFDs !=
(tdir_t)(NUMBER_OF_DIRS + NUMBER_OF_SUBIFDs) - number_of_sub_IFDs)
{

View File

@@ -351,6 +351,7 @@ int test_arbitrary_directrory_loading(unsigned int openMode)
char filename[128] = {0};
TIFF *tif;
uint64_t offsets_base[N_DIRECTORIES];
uint64_t off2;
int expected_original_dirnumber;
tdir_t expected_curdir = (tdir_t)(-1);
@@ -546,7 +547,7 @@ int test_arbitrary_directrory_loading(unsigned int openMode)
goto failure;
}
CHECKCURDIRNUM_M(tif, 2, __LINE__);
uint64_t off2 = TIFFCurrentDirOffset(tif);
off2 = TIFFCurrentDirOffset(tif);
/* Note that dirnum = 2 is deleted here since TIFFUnlinkDirectory()
* starts with 1 instead of 0. */
if (!TIFFUnlinkDirectory(tif, 3))
@@ -808,6 +809,10 @@ int test_SubIFD_directrory_handling(unsigned int openMode)
int iIFD = 0, iSubIFD = 0;
TIFF *tif;
int expected_original_dirnumber;
tdir_t expected_curdir;
tdir_t numberOfMainIFDs;
tdir_t currentDirNumber;
int blnRead;
if (openMode >= (sizeof(openModeStrings) / sizeof(openModeStrings[0])))
{
@@ -888,7 +893,7 @@ int test_SubIFD_directrory_handling(unsigned int openMode)
goto failure;
}
tdir_t numberOfMainIFDs = TIFFNumberOfDirectories(tif);
numberOfMainIFDs = TIFFNumberOfDirectories(tif);
CHECKCURDIRNUM_M(tif, 0, __LINE__);
if (numberOfMainIFDs != (tdir_t)N_DIRECTORIES - number_of_sub_IFDs)
{
@@ -899,10 +904,10 @@ int test_SubIFD_directrory_handling(unsigned int openMode)
goto failure;
}
tdir_t currentDirNumber = TIFFCurrentDirectory(tif);
currentDirNumber = TIFFCurrentDirectory(tif);
/* The first directory is already read through TIFFOpen() */
int blnRead = 0;
blnRead = 0;
expected_original_dirnumber = 1;
do
{
@@ -1001,7 +1006,7 @@ int test_SubIFD_directrory_handling(unsigned int openMode)
/*-- Test TIFFCheckpointDirectory() with SubIFDs --
* However, SubIFDs cannot be re-written to another location because
* TIFFRewriteDirectory() does not support SubIFDs. Overwriting works. */
tdir_t expected_curdir = TIFFNumberOfDirectories(tif);
expected_curdir = TIFFNumberOfDirectories(tif);
TIFFCreateDirectory(tif);
if (write_data_to_current_directory(tif, expected_curdir, false))
{
@@ -1226,6 +1231,7 @@ int test_rewrite_lastdir_offset(unsigned int openMode)
char filename[128] = {0};
int i, count;
TIFF *tif;
uint64_t off1, off2;
if (openMode >= (sizeof(openModeStrings) / sizeof(openModeStrings[0])))
{
@@ -1276,7 +1282,7 @@ int test_rewrite_lastdir_offset(unsigned int openMode)
* speed up of TIFFSetDirectory() with directly getting the offset that
* list.
*/
uint64_t off1 = TIFFCurrentDirOffset(tif);
off1 = TIFFCurrentDirOffset(tif);
if (write_data_to_current_directory(tif, 4, false))
{
fprintf(stderr, "Can't write data to fifth directory in %s\n",
@@ -1294,7 +1300,7 @@ int test_rewrite_lastdir_offset(unsigned int openMode)
fprintf(stderr, "Can't set %d.th directory from %s\n", i, filename);
goto failure;
}
uint64_t off2 = TIFFCurrentDirOffset(tif);
off2 = TIFFCurrentDirOffset(tif);
if (!is_requested_directory(tif, i, filename))
{
goto failure;
@@ -2110,6 +2116,8 @@ int test_curdircount_setting(unsigned int openMode)
#define N_DIRECTORIES_2 2
char filename[128] = {0};
tdir_t expected_curdir = (tdir_t)(-1);
TIFF *tif;
tdir_t numdir;
if (openMode >= (sizeof(openModeStrings) / sizeof(openModeStrings[0])))
{
@@ -2123,7 +2131,7 @@ int test_curdircount_setting(unsigned int openMode)
unlink(filename);
/* Create a file and write N_DIRECTORIES_2 directories to it. */
TIFF *tif = TIFFOpen(filename, openModeStrings[openMode]);
tif = TIFFOpen(filename, openModeStrings[openMode]);
if (!tif)
{
fprintf(stderr, "Can't create %s\n", filename);
@@ -2157,7 +2165,7 @@ int test_curdircount_setting(unsigned int openMode)
TIFFCreateDirectory(tif);
TIFFWriteDirectory(tif);
CHECKCURDIRNUM_M(tif, 2, __LINE__);
tdir_t numdir = TIFFNumberOfDirectories(tif);
numdir = TIFFNumberOfDirectories(tif);
TIFFClose(tif);
/* Testcase iswrittentofile=0 for SetSubDirectory(0). */

View File

@@ -150,6 +150,9 @@ int write_basic_IFD_data(TIFF **ptif, const char *filename, int wrtTransferFunct
unsigned char buf[3] = {0, 127, 255};
int retval = 0;
uint8_t *bufLine = NULL;
uint16_t extraSamples[4] = {EXTRASAMPLE_UNSPECIFIED, EXTRASAMPLE_UNSPECIFIED, EXTRASAMPLE_UNSPECIFIED,
EXTRASAMPLE_UNSPECIFIED};
size_t bufLen;
TIFF *tif = TIFFOpen(filename, "w");
*ptif = tif;
if (!tif)
@@ -206,8 +209,6 @@ int write_basic_IFD_data(TIFF **ptif, const char *filename, int wrtTransferFunct
}
/* Set ExtraSamples thus SamplesPerPixel for transfer functions is reduced by one. */
uint16_t extraSamples[4] = {EXTRASAMPLE_UNSPECIFIED, EXTRASAMPLE_UNSPECIFIED, EXTRASAMPLE_UNSPECIFIED,
EXTRASAMPLE_UNSPECIFIED};
if (nExtraSamples > 0 && nExtraSamples < 4)
{
if (!TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, nExtraSamples, extraSamples))
@@ -228,7 +229,7 @@ int write_basic_IFD_data(TIFF **ptif, const char *filename, int wrtTransferFunct
}
/* Setup buffer for image line */
size_t bufLen = (size_t)width * spp * (bps + 7) / 8;
bufLen = (size_t)width * spp * (bps + 7) / 8;
bufLine = (uint8_t *)_TIFFmalloc(bufLen);
if (!bufLine)
{

View File

@@ -266,6 +266,12 @@ int write_test_tiff(TIFF *tif, const char *filenameRead)
uint16_t auxUint16 = 0;
uint32_t auxUint32 = 0;
int i;
uint64_t read_dir_offset = 0;
uint64_t dir_offset = 0;
uint64_t dir_offset_GPS = 0;
uint64_t dir_offset_EXIF = 0;
const TIFFFieldArray *tFieldArray;
uint32_t iDataCnt = 0;
/*-- Fill test data arrays for writing and later comparison when written
* tags are checked. */
@@ -338,7 +344,7 @@ int write_test_tiff(TIFF *tif, const char *filenameRead)
* space for final dir_offset value,
* which is properly written at the end.
*/
uint64_t dir_offset = 0; /* Zero, in case no Custom-IFD is written */
dir_offset = 0; /* Zero, in case no Custom-IFD is written */
if (!TIFFSetField(tif, TIFFTAG_GPSIFD, dir_offset))
{
@@ -352,11 +358,11 @@ int write_test_tiff(TIFF *tif, const char *filenameRead)
/*============ Write mostly all other TIFF tags ==================*/
/* Get array, where tag fields are defined. */
const TIFFFieldArray *tFieldArray = _TIFFGetFields();
tFieldArray = _TIFFGetFields();
/*-- write_all_tags() writes all tags automatically with the defined
* precision according to its set_get_field_type definition. --*/
uint32_t iDataCnt = 0;
iDataCnt = 0;
if (write_all_tags(tif, tFieldArray, listTagsNotToWrite,
NUM_ELEMENTS(listTagsNotToWrite), &iDataCnt))
{
@@ -427,7 +433,7 @@ int write_test_tiff(TIFF *tif, const char *filenameRead)
/*-- GPS - write custom directory GPS into file and
* get back the offset of GPS directory.
*/
uint64_t dir_offset_GPS = 0;
dir_offset_GPS = 0;
if (!TIFFWriteCustomDirectory(tif, &dir_offset_GPS))
{
fprintf(stderr, "TIFFWriteCustomDirectory() with GPS failed.\n");
@@ -492,7 +498,7 @@ int write_test_tiff(TIFF *tif, const char *filenameRead)
/*-- EXIF - write custom directory EXIF into file and
* get back the offset of EXIF directory.
*/
uint64_t dir_offset_EXIF = 0;
dir_offset_EXIF = 0;
if (!TIFFWriteCustomDirectory(tif, &dir_offset_EXIF))
{
fprintf(stderr, "TIFFWriteCustomDirectory() with EXIF failed.\n");
@@ -613,7 +619,7 @@ int write_test_tiff(TIFF *tif, const char *filenameRead)
* TIFFReadGPSDirectory().
* (this will destroy previously main directory fields in memory!)
*/
uint64_t read_dir_offset = 0;
read_dir_offset = 0;
fprintf(stderr, "----Read GPS tags ...\n");
if (!TIFFGetField(tif, TIFFTAG_GPSIFD, &read_dir_offset))
{
@@ -1327,6 +1333,7 @@ int read_all_tags(TIFF *tif, const TIFFFieldArray *tFieldArray,
switch (tSetFieldType)
{
case TIFF_SETGET_ASCII:
{
/* Either the stringlength is defined as a fixed length in
* .field_writecount or a NULL-terminated string is used. */
if (!TIFFGetField(tif, tTag, &pAscii))
@@ -1355,6 +1362,7 @@ int read_all_tags(TIFF *tif, const TIFFFieldArray *tFieldArray,
GOTOFAILURE
}
break;
}
case TIFF_SETGET_C16_ASCII:
if (tTag == TIFFTAG_INKNAMES)
{