# [PATCH] Bug #45799 - imagepng() crashes on empty image

- Thread: https://externals.io/message/43289
- Original archive: https://news-web.php.net/php.internals/43289
- Started by Martin McNickle on 2009-03-10T11:17:13+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #43289 — Martin McNickle — 2009-03-10T11:17:13+00:00

URL: https://externals.io/message/43289#43289

Hi all,

I've created a simple patch for Bug #45799 [1]

It employs a simple check to see if there are any colors associated with
the image before outputting the PNG header.

The patch is against CVS 5.3, shown below for convenience. I can create
patches for the other branches if required. The tests pass.

This is my first patch to PHP, so I've done anything wrong, give me some
pointers as to how to fix it.

Thanks,

-- Martin McNickle

[1] http://bugs.php.net/bug.php?id=45799

Index: ext/gd/libgd/gd_png.c
===================================================================
RCS file: /repository/php-src/ext/gd/libgd/gd_png.c,v
retrieving revision 1.17.4.2.2.5.2.3
diff -u -r1.17.4.2.2.5.2.3 gd_png.c
--- ext/gd/libgd/gd_png.c	14 Jan 2009 10:16:15 -0000	1.17.4.2.2.5.2.3
+++ ext/gd/libgd/gd_png.c	10 Mar 2009 11:12:02 -0000
@@ -650,6 +650,12 @@
 		png_set_PLTE(png_ptr, info_ptr, palette, colors);
 	}
 
+	if (colors <= 0 && !im->trueColor) {
+		php_gd_error("gd-png error: Invalid number of colors in
palette./martin");
+		png_destroy_write_struct (&png_ptr, &info_ptr);
+		return;
+	}
+
 	/* write out the PNG header info (everything up to first IDAT) */
 	png_write_info(png_ptr, info_ptr);

---

## #43290 — Pierre Joye — 2009-03-10T11:19:42+00:00

URL: https://externals.io/message/43289#43290 · In reply to: #43289

hi,

Please post a link to the patch in the bug tracker directly.

Thanks for your work,

Cheers,

On Tue, Mar 10, 2009 at 12:17 PM, Martin McNickle
<mmcnicklebugs@googlemail.com> wrote:
> Hi all,
>
> I've created a simple patch for Bug #45799 [1]
>
> It employs a simple check to see if there are any colors associated with
> the image before outputting the PNG header.
>
> The patch is against CVS 5.3, shown below for convenience. I can create
> patches for the other branches if required. The tests pass.
>
> This is my first patch to PHP, so I've done anything wrong, give me some
> pointers as to how to fix it.
>
> Thanks,
>
> -- Martin McNickle
>
> [1] http://bugs.php.net/bug.php?id=45799
>
> Index: ext/gd/libgd/gd_png.c
> ===================================================================
> RCS file: /repository/php-src/ext/gd/libgd/gd_png.c,v
> retrieving revision 1.17.4.2.2.5.2.3
> diff -u -r1.17.4.2.2.5.2.3 gd_png.c
> --- ext/gd/libgd/gd_png.c       14 Jan 2009 10:16:15 -0000      1.17.4.2.2.5.2.3
> +++ ext/gd/libgd/gd_png.c       10 Mar 2009 11:12:02 -0000
> @@ -650,6 +650,12 @@
>                png_set_PLTE(png_ptr, info_ptr, palette, colors);
>        }
>
> +       if (colors <= 0 && !im->trueColor) {
> +               php_gd_error("gd-png error: Invalid number of colors in
> palette./martin");
> +               png_destroy_write_struct (&png_ptr, &info_ptr);
> +               return;
> +       }
> +
>        /* write out the PNG header info (everything up to first IDAT) */
>        png_write_info(png_ptr, info_ptr);
>
>
>
> --
> 
> 
>



-- 
Pierre

http://blog.thepimp.net | http://www.libgd.org

---

## #43291 — Martin McNickle — 2009-03-10T11:27:38+00:00

URL: https://externals.io/message/43289#43291 · In reply to: #43290

Patch is in the tracker now.

-- Martin


> hi,
> 
> Please post a link to the patch in the bug tracker directly.
> 
> Thanks for your work,
> 
> Cheers,
> 
> On Tue, Mar 10, 2009 at 12:17 PM, Martin McNickle
> <mmcnicklebugs@googlemail.com> wrote:
> > […]
