Skip to content

Commit ea6f6bd

Browse files
SuLausrouault
authored andcommittedNov 19, 2024
Update test/test_directory.c not to fail on big-endian machines. Fix memory leaks
Closes #652 et #656
1 parent 97aa7f4 commit ea6f6bd

File tree

1 file changed

+56
-11
lines changed

1 file changed

+56
-11
lines changed
 

‎test/test_directory.c

+56-11
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,7 @@ int test_rewrite_lastdir_offset(unsigned int openMode)
13651365
filename, N_DIRECTORIES, count);
13661366
goto failure;
13671367
}
1368+
/* hint: file was closed by count_directories() */
13681369
unlink(filename);
13691370
return 0;
13701371

@@ -1511,6 +1512,8 @@ int test_lastdir_offset(unsigned int openMode)
15111512
}
15121513
}
15131514
}
1515+
/* hint: files are always closed by count_directories() and
1516+
* get_dir_offsets() */
15141517
unlink(filename_optimized);
15151518
unlink(filename_non_optimized);
15161519
return 0;
@@ -1977,60 +1980,99 @@ int test_current_dirnum_incrementing(int testcase, unsigned int openMode)
19771980
TIFFSetSubDirectory(tif, 0);
19781981
CHECKCURDIRNUM_M(tif, (tdir_t)(-1), __LINE__);
19791982

1980-
/*-- Patch offset of IFD2 to not existing IFD3 without entries.
1981-
* Thus TIFFFetchDirectory() will fail. --*/
1983+
/*-- Patch offset of IFD2 to not existing IFD3 without entries.
1984+
* Thus TIFFFetchDirectory() will fail. --*/
19821985
#define TIFFReadFile_M(tif, buf, size) \
19831986
((*TIFFGetReadProc(tif))(TIFFClientdata(tif), (buf), (size)));
19841987
#define TIFFWriteFile_M(tif, buf, size) \
19851988
((*TIFFGetWriteProc(tif))(TIFFClientdata(tif), (buf), (size)));
19861989
#define TIFFSeekFile_M(tif, off, whence) \
19871990
((*TIFFGetSeekProc(tif))(TIFFClientdata(tif), (off), (whence)));
19881991

1989-
/* Code below does only handle Classic-TIFF without swapping". */
1990-
if (!(TIFFIsByteSwapped(tif) || TIFFIsBigTIFF(tif)))
1991-
{
1992+
/* ---------------------------------------------------------------------
1993+
* Test IFD index incrementing in case the functions return with certain
1994+
* errors. To provoke that errors, the file is patched by writing bytes
1995+
* directly into the file. Therefore, code below does only handle
1996+
* Classic-TIFF and little-endian files.
1997+
* The code works also on big endian machines, which have to swap some
1998+
* directly read/written values.
1999+
* --------------------------------------------------------------------- */
2000+
if (!(TIFFIsBigEndian(tif) || TIFFIsBigTIFF(tif)))
2001+
{
2002+
/* Patch nextIFDOffset of IFD2, which is 0, with offset to itself.
2003+
* This generates an IFD3 without any elements at the end of file.
2004+
* Reading IFD3 should provoke reading error. */
19922005
uint64_t ss = TIFFSeekFile_M(tif, offsetBase[2], 0);
19932006
uint16_t cnt = 0;
19942007
uint64_t rr = TIFFReadFile_M(tif, &cnt, 2);
2008+
if (TIFFIsByteSwapped(tif))
2009+
TIFFSwabShort(&cnt);
19952010
ss = TIFFSeekFile_M(tif, offsetBase[2] + cnt * 12 + 2, 0);
19962011
uint32_t wt = (uint32_t)ss;
2012+
if (TIFFIsByteSwapped(tif))
2013+
TIFFSwabLong(&wt);
19972014
rr = TIFFWriteFile_M(tif, &wt, 4);
19982015
(void)rr;
19992016

20002017
/* Now there are offsets to four IFDs in the file, where the last one is
2001-
* not existing and has a non-valid dircount and entries behind EOF. */
2018+
* not existing and has a non-valid dircount and entries behind EOF.
2019+
* (dircount is 458 (as offset) */
20022020
fprintf(stderr, "----- Expect error messages about 'Error fetching "
20032021
"directory link.' -----\n");
2004-
/* TIFFNumberOfDirectories() returns 3 */
2022+
/* TIFFNumberOfDirectories() returns 3 and omits the invalid fourth IFD.
2023+
*/
20052024
lastdir = TIFFNumberOfDirectories(tif);
20062025
TIFFSetDirectory(tif, 0);
20072026
CHECKCURDIRNUM_M(tif, 0, __LINE__);
2027+
2028+
/* TIFFSetDirectory(3) fails with error messages:
2029+
* TIFFFetchDirectory: test_current_dirnum_incrementing_wl.tif:
2030+
* Can not read TIFF directory.
2031+
* TIFFReadDirectory: Failed to read directory at offset 458. */
20082032
fprintf(stderr, "----- Expect error messages about 'Cannot read TIFF "
20092033
"directory.' -----\n");
20102034
if (TIFFSetDirectory(tif, 3))
20112035
{
20122036
fprintf(stderr,
2013-
"TIFFSetDirectory(3) for IFD4 was expected to fail but "
2037+
"TIFFSetDirectory(3) for IFD3 was expected to fail but "
20142038
"succeeded for %s "
20152039
"at %d\n",
20162040
filename, __LINE__);
20172041
goto failure;
20182042
}
2043+
20192044
/* Fails in 4.6.0 */
2045+
/* Reading invalid IFD 3 leads to an error and was not read in.
2046+
* Therefore, curdir shall be 65535 (non-existing directory) */
20202047
CHECKCURDIRNUM_M(tif, (tdir_t)(-1), __LINE__);
20212048
offsetBase[3] = TIFFCurrentDirOffset(tif);
20222049

2023-
/* Point IFD3 to a location within the file, where it has now a
2024-
* non-valid dircount=0. */
2050+
/* Point IFD3 to a location within the file, where it has now for
2051+
* little-endian TIFF files a non-valid dircount=0, which leads also to
2052+
* an error and the IFD is not read in. */
20252053
ss = TIFFSeekFile_M(tif, offsetBase[2] + cnt * 12 + 2, 0);
20262054
wt = (uint32_t)(offsetBase[1] + 8);
2055+
// wt = (uint32_t)(ss + 400);
2056+
if (TIFFIsByteSwapped(tif))
2057+
TIFFSwabLong(&wt);
20272058
rr = TIFFWriteFile_M(tif, &wt, 4);
2059+
20282060
fprintf(stderr, "----- Expect error messages about 'Error fetching "
20292061
"directory link.' -----\n");
2030-
/* TIFFNumberOfDirectories() returns now 4 */
2062+
/* TIFFNumberOfDirectories() returns now 4, because for an IFD linked
2063+
* list dircount=0 is not treated as an error and there is an offset
2064+
* (=1) to a next IFD. Then, at the fifth IFD a link error occurs. */
20312065
lastdir = TIFFNumberOfDirectories(tif);
20322066
TIFFSetDirectory(tif, 0);
20332067
CHECKCURDIRNUM_M(tif, 0, __LINE__);
2068+
2069+
/* TIFFSetDirectory(3) fails with error messages:
2070+
* test_current_dirnum_incrementing_wl.tif: Failed to allocate
2071+
* memory for to read TIFF directory (0 elements of 12 bytes each).
2072+
* TIFFReadDirectory: Failed to read directory at offset 178.
2073+
* The IFD 3 is not read in and curdir is set to 65535 (non-existing
2074+
* directory).
2075+
*/
20342076
fprintf(stderr,
20352077
"----- Expect error messages about 'Failed to allocate "
20362078
"memory for to read TIFF directory.' AND 'Failed to read "
@@ -2044,10 +2086,12 @@ int test_current_dirnum_incrementing(int testcase, unsigned int openMode)
20442086
filename, __LINE__);
20452087
goto failure;
20462088
}
2089+
20472090
/* Fails in 4.6.0 */
20482091
CHECKCURDIRNUM_M(tif, (tdir_t)(-1), __LINE__);
20492092
}
20502093

2094+
TIFFClose(tif);
20512095
unlink(filename);
20522096
return 0;
20532097

@@ -2136,6 +2180,7 @@ int test_curdircount_setting(unsigned int openMode)
21362180
CHECKCURDIRNUM_M(tif, (tdir_t)(-1), __LINE__);
21372181
}
21382182

2183+
TIFFClose(tif);
21392184
unlink(filename);
21402185
return 0;
21412186

0 commit comments

Comments
 (0)