@@ -1365,6 +1365,7 @@ int test_rewrite_lastdir_offset(unsigned int openMode)
1365
1365
filename , N_DIRECTORIES , count );
1366
1366
goto failure ;
1367
1367
}
1368
+ /* hint: file was closed by count_directories() */
1368
1369
unlink (filename );
1369
1370
return 0 ;
1370
1371
@@ -1511,6 +1512,8 @@ int test_lastdir_offset(unsigned int openMode)
1511
1512
}
1512
1513
}
1513
1514
}
1515
+ /* hint: files are always closed by count_directories() and
1516
+ * get_dir_offsets() */
1514
1517
unlink (filename_optimized );
1515
1518
unlink (filename_non_optimized );
1516
1519
return 0 ;
@@ -1977,60 +1980,99 @@ int test_current_dirnum_incrementing(int testcase, unsigned int openMode)
1977
1980
TIFFSetSubDirectory (tif , 0 );
1978
1981
CHECKCURDIRNUM_M (tif , (tdir_t )(-1 ), __LINE__ );
1979
1982
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. --*/
1982
1985
#define TIFFReadFile_M (tif , buf , size ) \
1983
1986
((*TIFFGetReadProc(tif))(TIFFClientdata(tif), (buf), (size)));
1984
1987
#define TIFFWriteFile_M (tif , buf , size ) \
1985
1988
((*TIFFGetWriteProc(tif))(TIFFClientdata(tif), (buf), (size)));
1986
1989
#define TIFFSeekFile_M (tif , off , whence ) \
1987
1990
((*TIFFGetSeekProc(tif))(TIFFClientdata(tif), (off), (whence)));
1988
1991
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. */
1992
2005
uint64_t ss = TIFFSeekFile_M (tif , offsetBase [2 ], 0 );
1993
2006
uint16_t cnt = 0 ;
1994
2007
uint64_t rr = TIFFReadFile_M (tif , & cnt , 2 );
2008
+ if (TIFFIsByteSwapped (tif ))
2009
+ TIFFSwabShort (& cnt );
1995
2010
ss = TIFFSeekFile_M (tif , offsetBase [2 ] + cnt * 12 + 2 , 0 );
1996
2011
uint32_t wt = (uint32_t )ss ;
2012
+ if (TIFFIsByteSwapped (tif ))
2013
+ TIFFSwabLong (& wt );
1997
2014
rr = TIFFWriteFile_M (tif , & wt , 4 );
1998
2015
(void )rr ;
1999
2016
2000
2017
/* 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) */
2002
2020
fprintf (stderr , "----- Expect error messages about 'Error fetching "
2003
2021
"directory link.' -----\n" );
2004
- /* TIFFNumberOfDirectories() returns 3 */
2022
+ /* TIFFNumberOfDirectories() returns 3 and omits the invalid fourth IFD.
2023
+ */
2005
2024
lastdir = TIFFNumberOfDirectories (tif );
2006
2025
TIFFSetDirectory (tif , 0 );
2007
2026
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. */
2008
2032
fprintf (stderr , "----- Expect error messages about 'Cannot read TIFF "
2009
2033
"directory.' -----\n" );
2010
2034
if (TIFFSetDirectory (tif , 3 ))
2011
2035
{
2012
2036
fprintf (stderr ,
2013
- "TIFFSetDirectory(3) for IFD4 was expected to fail but "
2037
+ "TIFFSetDirectory(3) for IFD3 was expected to fail but "
2014
2038
"succeeded for %s "
2015
2039
"at %d\n" ,
2016
2040
filename , __LINE__ );
2017
2041
goto failure ;
2018
2042
}
2043
+
2019
2044
/* 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) */
2020
2047
CHECKCURDIRNUM_M (tif , (tdir_t )(-1 ), __LINE__ );
2021
2048
offsetBase [3 ] = TIFFCurrentDirOffset (tif );
2022
2049
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. */
2025
2053
ss = TIFFSeekFile_M (tif , offsetBase [2 ] + cnt * 12 + 2 , 0 );
2026
2054
wt = (uint32_t )(offsetBase [1 ] + 8 );
2055
+ // wt = (uint32_t)(ss + 400);
2056
+ if (TIFFIsByteSwapped (tif ))
2057
+ TIFFSwabLong (& wt );
2027
2058
rr = TIFFWriteFile_M (tif , & wt , 4 );
2059
+
2028
2060
fprintf (stderr , "----- Expect error messages about 'Error fetching "
2029
2061
"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. */
2031
2065
lastdir = TIFFNumberOfDirectories (tif );
2032
2066
TIFFSetDirectory (tif , 0 );
2033
2067
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
+ */
2034
2076
fprintf (stderr ,
2035
2077
"----- Expect error messages about 'Failed to allocate "
2036
2078
"memory for to read TIFF directory.' AND 'Failed to read "
@@ -2044,10 +2086,12 @@ int test_current_dirnum_incrementing(int testcase, unsigned int openMode)
2044
2086
filename , __LINE__ );
2045
2087
goto failure ;
2046
2088
}
2089
+
2047
2090
/* Fails in 4.6.0 */
2048
2091
CHECKCURDIRNUM_M (tif , (tdir_t )(-1 ), __LINE__ );
2049
2092
}
2050
2093
2094
+ TIFFClose (tif );
2051
2095
unlink (filename );
2052
2096
return 0 ;
2053
2097
@@ -2136,6 +2180,7 @@ int test_curdircount_setting(unsigned int openMode)
2136
2180
CHECKCURDIRNUM_M (tif , (tdir_t )(-1 ), __LINE__ );
2137
2181
}
2138
2182
2183
+ TIFFClose (tif );
2139
2184
unlink (filename );
2140
2185
return 0 ;
2141
2186
0 commit comments