Skip to content

Commit 7d96dca

Browse files
committed
Fix #55005: imagepolygon num_points requirement
We actually have to check `$num_points` instead of `2*count($points)`, because the latter may be greater than the former, but not all elements of `$points` are guaranteed to be used. This allowed to pass arrays with excess elements to draw polygons with less than three vertices. While the current implementation of `gdImagePolygon()` and friends would allow us to draw monogons and digons, we don't allow that anymore, because the respective drawing primitives work slightly different (e.g. drawing lines support anti-aliasing, but drawing general polygons does not). To minimize the BC break, we do not fix this longstanding issue for PHP 7, but target PHP 8 only.
1 parent 4d067d8 commit 7d96dca

File tree

5 files changed

+24
-49
lines changed

5 files changed

+24
-49
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ PHP NEWS
1717
that allow global flag to configure query() or evaluate() calls.
1818

1919
- GD:
20+
. Fixed bug #55005 (imagepolygon num_points requirement). (cmb)
2021
. Replaced gd resources with objects. (Mark Randall)
2122
. Removed deprecated image2wbmp(). (cmb)
2223
. Removed deprecated png2wbmp() and jpeg2wbmp(). (cmb)

ext/gd/gd.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,13 +2745,8 @@ static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled)
27452745
col = COL;
27462746

27472747
nelem = zend_hash_num_elements(Z_ARRVAL_P(POINTS));
2748-
if (nelem < 6) {
2749-
zend_value_error("You must have at least 3 points in your array");
2750-
return;
2751-
}
2752-
2753-
if (npoints <= 0) {
2754-
zend_value_error("You must give a positive number of points");
2748+
if (npoints < 3) {
2749+
zend_value_error("Polygon must have at least 3 points");
27552750
return;
27562751
}
27572752

ext/gd/tests/bug55005.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Bug #55005 (imagepolygon num_points requirement)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gd')) die('skip gd extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
require_once __DIR__ . '/func.inc';
10+
11+
$g = imagecreate(300, 300);
12+
$bgnd = imagecolorallocate($g, 255, 255, 255);
13+
$fgnd = imagecolorallocate($g, 0, 0, 0);
14+
trycatch_dump(
15+
fn () => imagefilledpolygon($g, array(100,10, 100,100, 180,100), 2, $fgnd),
16+
fn () => imagepolygon($g, array(200,10, 200,100, 280,100), 2, $fgnd)
17+
);
18+
?>
19+
--EXPECT--
20+
!! [ValueError] Polygon must have at least 3 points
21+
!! [ValueError] Polygon must have at least 3 points

ext/gd/tests/imagefilledpolygon_negative.phpt

Lines changed: 0 additions & 21 deletions
This file was deleted.

ext/gd/tests/imagepolygon_negative.phpt

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)