Blame SOURCES/libtiff-CVE-2016-3991.patch

460672
diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
460672
index ea4f7a1..f04e2eb 100644
460672
--- a/tools/tiffcrop.c
460672
+++ b/tools/tiffcrop.c
460672
@@ -798,6 +798,11 @@ static int readContigTilesIntoBuffer (TIFF* in, uint8* buf,
460672
     }
460672
 
460672
   tile_buffsize = tilesize;
460672
+  if (tilesize == 0 || tile_rowsize == 0)
460672
+  {
460672
+     TIFFError("readContigTilesIntoBuffer", "Tile size or tile rowsize is zero");
460672
+     exit(-1);
460672
+  }
460672
 
460672
   if (tilesize < (tsize_t)(tl * tile_rowsize))
460672
     {
460672
@@ -807,7 +812,12 @@ static int readContigTilesIntoBuffer (TIFF* in, uint8* buf,
460672
               tilesize, tl * tile_rowsize);
460672
 #endif
460672
     tile_buffsize = tl * tile_rowsize;
460672
-    } 
460672
+    if (tl != (tile_buffsize / tile_rowsize))
460672
+    {
460672
+    	TIFFError("readContigTilesIntoBuffer", "Integer overflow when calculating buffer size.");
460672
+        exit(-1);
460672
+    }
460672
+    }
460672
 
460672
   tilebuf = _TIFFmalloc(tile_buffsize);
460672
   if (tilebuf == 0)
460672
@@ -1210,6 +1220,12 @@ static int writeBufferToContigTiles (TIFF* out, uint8* buf, uint32 imagelength,
460672
       !TIFFGetField(out, TIFFTAG_BITSPERSAMPLE, &bps) )
460672
       return 1;
460672
 
460672
+  if (tilesize == 0 || tile_rowsize == 0 || tl == 0 || tw == 0)
460672
+  {
460672
+    TIFFError("writeBufferToContigTiles", "Tile size, tile row size, tile width, or tile length is zero");
460672
+    exit(-1);
460672
+  }
460672
+  
460672
   tile_buffsize = tilesize;
460672
   if (tilesize < (tsize_t)(tl * tile_rowsize))
460672
     {
460672
@@ -1219,6 +1235,11 @@ static int writeBufferToContigTiles (TIFF* out, uint8* buf, uint32 imagelength,
460672
               tilesize, tl * tile_rowsize);
460672
 #endif
460672
     tile_buffsize = tl * tile_rowsize;
460672
+    if (tl != tile_buffsize / tile_rowsize)
460672
+    {
460672
+	TIFFError("writeBufferToContigTiles", "Integer overflow when calculating buffer size");
460672
+	exit(-1);
460672
+    }
460672
     }
460672
 
460672
   tilebuf = _TIFFmalloc(tile_buffsize);
460672
@@ -5931,12 +5952,27 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
460672
     TIFFGetField(in, TIFFTAG_TILELENGTH, &tl;;
460672
 
460672
     tile_rowsize  = TIFFTileRowSize(in);      
460672
+    if (ntiles == 0 || tlsize == 0 || tile_rowsize == 0)
460672
+    {
460672
+	TIFFError("loadImage", "File appears to be tiled, but the number of tiles, tile size, or tile rowsize is zero.");
460672
+	exit(-1);
460672
+    }
460672
     buffsize = tlsize * ntiles;
460672
+    if (tlsize != (buffsize / ntiles))
460672
+    {
460672
+	TIFFError("loadImage", "Integer overflow when calculating buffer size");
460672
+	exit(-1);
460672
+    }
460672
 
460672
-        
460672
     if (buffsize < (uint32)(ntiles * tl * tile_rowsize))
460672
       {
460672
       buffsize = ntiles * tl * tile_rowsize;
460672
+      if (ntiles != (buffsize / tl / tile_rowsize))
460672
+      {
460672
+	TIFFError("loadImage", "Integer overflow when calculating buffer size");
460672
+	exit(-1);
460672
+      }
460672
+      
460672
 #ifdef DEBUG2
460672
       TIFFError("loadImage",
460672
 	        "Tilesize %u is too small, using ntiles * tilelength * tilerowsize %lu",
460672
@@ -5955,8 +5991,25 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
460672
     TIFFGetFieldDefaulted(in, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
460672
     stsize = TIFFStripSize(in);
460672
     nstrips = TIFFNumberOfStrips(in);
460672
+    if (nstrips == 0 || stsize == 0)
460672
+    {
460672
+	TIFFError("loadImage", "File appears to be striped, but the number of stipes or stripe size is zero.");
460672
+	exit(-1);
460672
+    }
460672
+
460672
     buffsize = stsize * nstrips;
460672
-    
460672
+    if (stsize != (buffsize / nstrips))
460672
+    {
460672
+	TIFFError("loadImage", "Integer overflow when calculating buffer size");
460672
+	exit(-1);
460672
+    }
460672
+    uint32 buffsize_check;
460672
+    buffsize_check = ((length * width * spp * bps) + 7);
460672
+    if (length != ((buffsize_check - 7) / width / spp / bps))
460672
+    {
460672
+	TIFFError("loadImage", "Integer overflow detected.");
460672
+	exit(-1);
460672
+    }
460672
     if (buffsize < (uint32) (((length * width * spp * bps) + 7) / 8))
460672
       {
460672
       buffsize =  ((length * width * spp * bps) + 7) / 8;