Skip to content

Commit f46f73b

Browse files
committed
Merge branch 'avoid_IFD_overwriting_other_data_TIFFWriteDirectory' into 'master'
TIFFWriteDirectory: Avoid overwriting following data if an IFD is enlarged. See merge request libtiff/libtiff!565
2 parents af3dfc7 + 4c809cc commit f46f73b

8 files changed

+2349
-172
lines changed

libtiff/tif_dir.c

+10
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,16 @@ void TIFFFreeDirectory(TIFF *tif)
16521652

16531653
_TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
16541654
_TIFFmemset(&(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
1655+
1656+
/* Reset some internal parameters for IFD data size checking. */
1657+
tif->tif_dir.td_dirdatasize_read = 0;
1658+
tif->tif_dir.td_dirdatasize_write = 0;
1659+
if (tif->tif_dir.td_dirdatasize_offsets != NULL)
1660+
{
1661+
_TIFFfreeExt(tif, tif->tif_dir.td_dirdatasize_offsets);
1662+
tif->tif_dir.td_dirdatasize_offsets = NULL;
1663+
tif->tif_dir.td_dirdatasize_Noffsets = 0;
1664+
}
16551665
}
16561666
#undef CleanupField
16571667

libtiff/tif_dir.h

+24
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ typedef struct
6565
tif_dirread.c */
6666
} TIFFDirEntry;
6767

68+
typedef struct
69+
{
70+
uint64_t offset;
71+
uint64_t length;
72+
} TIFFEntryOffsetAndLength; /* auxiliary for evaluating size of IFD data */
73+
6874
/*
6975
* Internal format of a TIFF directory entry.
7076
*/
@@ -115,6 +121,9 @@ typedef struct
115121
#ifdef STRIPBYTECOUNTSORTED_UNUSED
116122
int td_stripbytecountsorted; /* is the bytecount array sorted ascending? */
117123
#endif
124+
/* Be aware that the parameters of td_stripoffset_entry and
125+
* td_stripbytecount_entry are swapped but tdir_offset is not
126+
* and has to be swapped when used. */
118127
TIFFDirEntry td_stripoffset_entry; /* for deferred loading */
119128
TIFFDirEntry td_stripbytecount_entry; /* for deferred loading */
120129
uint16_t td_nsubifd;
@@ -135,6 +144,21 @@ typedef struct
135144

136145
unsigned char
137146
td_deferstrilearraywriting; /* see TIFFDeferStrileArrayWriting() */
147+
148+
/* LibTIFF writes all data that does not fit into the IFD entries directly
149+
* after the IFD tag enty part. When reading, only the IFD data directly and
150+
* continuously behind the IFD tags is taken into account for the IFD data
151+
* size.*/
152+
uint64_t td_dirdatasize_write; /* auxiliary for evaluating size of IFD data
153+
to be written */
154+
uint64_t td_dirdatasize_read; /* auxiliary for evaluating size of IFD data
155+
read from file */
156+
uint32_t td_dirdatasize_Noffsets; /* auxiliary counter for
157+
tif_dir.td_dirdatasize_offsets array */
158+
TIFFEntryOffsetAndLength
159+
*td_dirdatasize_offsets; /* auxiliary array for all offsets of IFD tag
160+
entries with data outside the IFD tag
161+
entries. */
138162
} TIFFDirectory;
139163

140164
/*

0 commit comments

Comments
 (0)