diff libgfx.c @ 467:80d09ea85e75

Check for NULL pointers from png_get_tRNS and png_get_PLTE.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 04 Nov 2012 16:51:54 +0200
parents ffd5e730d313
children 0359697eeb46
line wrap: on
line diff
--- a/libgfx.c	Sun Nov 04 16:51:24 2012 +0200
+++ b/libgfx.c	Sun Nov 04 16:51:54 2012 +0200
@@ -602,20 +602,20 @@
         case PNG_COLOR_TYPE_PALETTE:
             png_get_PLTE(png_ptr, info_ptr, &palette, &ncolors);
             dmMsg(3, "PNG: Palette of %d colors found.\n", ncolors);            
-            if (ncolors <= 0)
-                goto error;
-
-            if (!dmImageAllocPalette(img, ncolors, -1))
+            if (ncolors > 0 && palette != NULL)
             {
-                res = DMERR_MALLOC;
-                goto error;
-            }
+                if (!dmImageAllocPalette(img, ncolors, -1))
+                {
+                    res = DMERR_MALLOC;
+                    goto error;
+                }
 
-            for (i = 0; i < img->ncolors; i++)
-            {
-                img->pal[i].r = palette[i].red;
-                img->pal[i].g = palette[i].green;
-                img->pal[i].b = palette[i].blue;
+                for (i = 0; i < img->ncolors; i++)
+                {
+                    img->pal[i].r = palette[i].red;
+                    img->pal[i].g = palette[i].green;
+                    img->pal[i].b = palette[i].blue;
+                }
             }
             break;
     }
@@ -624,11 +624,14 @@
         color_type == PNG_COLOR_TYPE_GRAY)
     {
         png_get_tRNS(png_ptr, info_ptr, &trans, &ntrans, NULL);
-        for (i = 0; i < img->ncolors && i < ntrans; i++)
+        if (trans != NULL && ntrans > 0)
         {
-            img->pal[i].a = trans[i];
-            if (img->ctransp < 0 && trans[i] == 0)
-                img->ctransp = i;
+            for (i = 0; i < img->ncolors && i < ntrans; i++)
+            {
+                img->pal[i].a = trans[i];
+                if (img->ctransp < 0 && trans[i] == 0)
+                    img->ctransp = i;
+            }
         }
     }