File tiff-4.7.0-test_directory.patch of Package tiff

From ea6f6bd7bccbe9a80327810993b8aae5587e1307 Mon Sep 17 00:00:00 2001
From: Su Laus <sulau@freenet.de>
Date: Tue, 19 Nov 2024 18:34:02 +0000
Subject: [PATCH] Update test/test_directory.c not to fail on big-endian
 machines. Fix memory leaks

Closes #652 et #656
---
 test/test_directory.c | 67 ++++++++++++++++++++++++++++++++++++-------
 1 file changed, 56 insertions(+), 11 deletions(-)

diff --git a/test/test_directory.c b/test/test_directory.c
index 0556da1ec..8cc376958 100644
--- a/test/test_directory.c
+++ b/test/test_directory.c
@@ -1365,6 +1365,7 @@ int test_rewrite_lastdir_offset(unsigned int openMode)
                 filename, N_DIRECTORIES, count);
         goto failure;
     }
+    /* hint: file was closed by count_directories() */
     unlink(filename);
     return 0;
 
@@ -1511,6 +1512,8 @@ int test_lastdir_offset(unsigned int openMode)
             }
         }
     }
+    /* hint: files are always closed by count_directories() and
+     * get_dir_offsets() */
     unlink(filename_optimized);
     unlink(filename_non_optimized);
     return 0;
@@ -1977,8 +1980,8 @@ int test_current_dirnum_incrementing(int testcase, unsigned int openMode)
     TIFFSetSubDirectory(tif, 0);
     CHECKCURDIRNUM_M(tif, (tdir_t)(-1), __LINE__);
 
-/*-- Patch offset of IFD2 to not existing IFD3 without entries.
- *   Thus TIFFFetchDirectory() will fail. --*/
+    /*-- Patch offset of IFD2 to not existing IFD3 without entries.
+     *   Thus TIFFFetchDirectory() will fail. --*/
 #define TIFFReadFile_M(tif, buf, size)                                         \
     ((*TIFFGetReadProc(tif))(TIFFClientdata(tif), (buf), (size)));
 #define TIFFWriteFile_M(tif, buf, size)                                        \
@@ -1986,51 +1989,90 @@ int test_current_dirnum_incrementing(int testcase, unsigned int openMode)
 #define TIFFSeekFile_M(tif, off, whence)                                       \
     ((*TIFFGetSeekProc(tif))(TIFFClientdata(tif), (off), (whence)));
 
-    /* Code below does only handle Classic-TIFF without swapping". */
-    if (!(TIFFIsByteSwapped(tif) || TIFFIsBigTIFF(tif)))
-    {
+    /* ---------------------------------------------------------------------
+     * Test IFD index incrementing in case the functions return with certain
+     * errors. To provoke that errors, the file is patched by writing bytes
+     * directly into the file. Therefore, code below does only handle
+     * Classic-TIFF and little-endian files.
+     * The code works also on big endian machines, which have to swap some
+     * directly read/written values.
+     * --------------------------------------------------------------------- */
+    if (!(TIFFIsBigEndian(tif) || TIFFIsBigTIFF(tif)))
+    {
+        /* Patch nextIFDOffset of IFD2, which is 0, with offset to itself.
+         * This generates an IFD3 without any elements at the end of file.
+         * Reading IFD3 should provoke reading error. */
         uint64_t ss = TIFFSeekFile_M(tif, offsetBase[2], 0);
         uint16_t cnt = 0;
         uint64_t rr = TIFFReadFile_M(tif, &cnt, 2);
+        if (TIFFIsByteSwapped(tif))
+            TIFFSwabShort(&cnt);
         ss = TIFFSeekFile_M(tif, offsetBase[2] + cnt * 12 + 2, 0);
         uint32_t wt = (uint32_t)ss;
+        if (TIFFIsByteSwapped(tif))
+            TIFFSwabLong(&wt);
         rr = TIFFWriteFile_M(tif, &wt, 4);
         (void)rr;
 
         /* Now there are offsets to four IFDs in the file, where the last one is
-         * not existing and has a non-valid dircount and entries behind EOF. */
+         * not existing and has a non-valid dircount and entries behind EOF.
+         * (dircount is 458 (as offset) */
         fprintf(stderr, "----- Expect error messages about 'Error fetching "
                         "directory link.' -----\n");
-        /* TIFFNumberOfDirectories() returns 3 */
+        /* TIFFNumberOfDirectories() returns 3 and omits the invalid fourth IFD.
+         */
         lastdir = TIFFNumberOfDirectories(tif);
         TIFFSetDirectory(tif, 0);
         CHECKCURDIRNUM_M(tif, 0, __LINE__);
+
+        /* TIFFSetDirectory(3) fails with error messages:
+         *   TIFFFetchDirectory: test_current_dirnum_incrementing_wl.tif:
+         *   Can not read TIFF directory.
+         *   TIFFReadDirectory: Failed to read directory at offset 458. */
         fprintf(stderr, "----- Expect error messages about 'Cannot read TIFF "
                         "directory.' -----\n");
         if (TIFFSetDirectory(tif, 3))
         {
             fprintf(stderr,
-                    "TIFFSetDirectory(3) for IFD4 was expected to fail but "
+                    "TIFFSetDirectory(3) for IFD3 was expected to fail but "
                     "succeeded for %s "
                     "at %d\n",
                     filename, __LINE__);
             goto failure;
         }
+
         /* Fails in 4.6.0 */
+        /* Reading invalid IFD 3 leads to an error and was not read in.
+         * Therefore, curdir shall be 65535 (non-existing directory) */
         CHECKCURDIRNUM_M(tif, (tdir_t)(-1), __LINE__);
         offsetBase[3] = TIFFCurrentDirOffset(tif);
 
-        /* Point IFD3 to a location within the file, where it has now a
-         * non-valid dircount=0. */
+        /* Point IFD3 to a location within the file, where it has now for
+         * little-endian TIFF files a non-valid dircount=0, which leads also to
+         * an error and the IFD is not read in. */
         ss = TIFFSeekFile_M(tif, offsetBase[2] + cnt * 12 + 2, 0);
         wt = (uint32_t)(offsetBase[1] + 8);
+        // wt = (uint32_t)(ss + 400);
+        if (TIFFIsByteSwapped(tif))
+            TIFFSwabLong(&wt);
         rr = TIFFWriteFile_M(tif, &wt, 4);
+
         fprintf(stderr, "----- Expect error messages about 'Error fetching "
                         "directory link.' -----\n");
-        /* TIFFNumberOfDirectories() returns now 4 */
+        /* TIFFNumberOfDirectories() returns now 4, because for an IFD linked
+         * list dircount=0 is not treated as an error and there is an offset
+         * (=1) to a next IFD. Then, at the fifth IFD a link error occurs. */
         lastdir = TIFFNumberOfDirectories(tif);
         TIFFSetDirectory(tif, 0);
         CHECKCURDIRNUM_M(tif, 0, __LINE__);
+
+        /* TIFFSetDirectory(3) fails with error messages:
+         *    test_current_dirnum_incrementing_wl.tif: Failed to allocate
+         *    memory for to read TIFF directory (0 elements of 12 bytes each).
+         *    TIFFReadDirectory: Failed to read directory at offset 178.
+         * The IFD 3 is not read in and curdir is set to 65535 (non-existing
+         * directory).
+         */
         fprintf(stderr,
                 "----- Expect error messages about 'Failed to allocate "
                 "memory for to read TIFF directory.' AND 'Failed to read "
@@ -2044,10 +2086,12 @@ int test_current_dirnum_incrementing(int testcase, unsigned int openMode)
                     filename, __LINE__);
             goto failure;
         }
+
         /* Fails in 4.6.0 */
         CHECKCURDIRNUM_M(tif, (tdir_t)(-1), __LINE__);
     }
 
+    TIFFClose(tif);
     unlink(filename);
     return 0;
 
@@ -2136,6 +2180,7 @@ int test_curdircount_setting(unsigned int openMode)
         CHECKCURDIRNUM_M(tif, (tdir_t)(-1), __LINE__);
     }
 
+    TIFFClose(tif);
     unlink(filename);
     return 0;
 
-- 
GitLab

openSUSE Build Service is sponsored by