63a68f4
Index: src/common/imagpng.cpp
63a68f4
===================================================================
63a68f4
--- src/common/imagpng.cpp	(revision 60874)
63a68f4
+++ src/common/imagpng.cpp	(revision 60875)
63a68f4
@@ -568,18 +568,16 @@
63a68f4
     if (!image->Ok())
63a68f4
         goto error;
63a68f4
 
63a68f4
-    lines = (unsigned char **)malloc( (size_t)(height * sizeof(unsigned char *)) );
63a68f4
+    // initialize all line pointers to NULL to ensure that they can be safely
63a68f4
+    // free()d if an error occurs before all of them could be allocated
63a68f4
+    lines = (unsigned char **)calloc(height, sizeof(unsigned char *));
63a68f4
     if ( !lines )
63a68f4
         goto error;
63a68f4
 
63a68f4
     for (i = 0; i < height; i++)
63a68f4
     {
63a68f4
         if ((lines[i] = (unsigned char *)malloc( (size_t)(width * (sizeof(unsigned char) * 4)))) == NULL)
63a68f4
-        {
63a68f4
-            for ( unsigned int n = 0; n < i; n++ )
63a68f4
-                free( lines[n] );
63a68f4
             goto error;
63a68f4
-        }
63a68f4
     }
63a68f4
 
63a68f4
     png_read_image( png_ptr, lines );
63a68f4
Index: src/common/imagtiff.cpp
63a68f4
===================================================================
63a68f4
--- src/common/imagtiff.cpp	(revision 60875)
63a68f4
+++ src/common/imagtiff.cpp	(revision 60876)
63a68f4
@@ -261,7 +261,6 @@
63a68f4
     }
63a68f4
 
63a68f4
     uint32 w, h;
63a68f4
-    uint32 npixels;
63a68f4
     uint32 *raster;
63a68f4
 
63a68f4
     TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w );
63a68f4
@@ -275,10 +274,21 @@
63a68f4
                            (samplesInfo[0] == EXTRASAMPLE_ASSOCALPHA ||
63a68f4
                             samplesInfo[0] == EXTRASAMPLE_UNASSALPHA));
63a68f4
 
63a68f4
-    npixels = w * h;
63a68f4
+    // guard against integer overflow during multiplication which could result
63a68f4
+    // in allocating a too small buffer and then overflowing it
63a68f4
+    const double bytesNeeded = w * h * sizeof(uint32);
63a68f4
+    if ( bytesNeeded >= 4294967295U /* UINT32_MAX */ )
63a68f4
+    {
63a68f4
+        if ( verbose )
63a68f4
+            wxLogError( _("TIFF: Image size is abnormally big.") );
63a68f4
 
63a68f4
-    raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) );
63a68f4
+        TIFFClose(tif);
63a68f4
 
63a68f4
+        return false;
63a68f4
+    }
63a68f4
+
63a68f4
+    raster = (uint32*) _TIFFmalloc( bytesNeeded );
63a68f4
+
63a68f4
     if (!raster)
63a68f4
     {
63a68f4
         if (verbose)
63a68f4
Index: src/common/imagtiff.cpp
63a68f4
===================================================================
63a68f4
--- src/common/imagtiff.cpp	(revision 60896)
63a68f4
+++ src/common/imagtiff.cpp	(revision 60897)
63a68f4
@@ -276,7 +276,7 @@
63a68f4
 
63a68f4
     // guard against integer overflow during multiplication which could result
63a68f4
     // in allocating a too small buffer and then overflowing it
63a68f4
-    const double bytesNeeded = w * h * sizeof(uint32);
63a68f4
+    const double bytesNeeded = (double)w * (double)h * sizeof(uint32);
63a68f4
     if ( bytesNeeded >= 4294967295U /* UINT32_MAX */ )
63a68f4
     {
63a68f4
         if ( verbose )