Skip to content

Commit cb535f0

Browse files
committed
Fix GH-20660: imageellipse()/imagefilledellipse() overflow
1 parent 1f1147a commit cb535f0

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

ext/gd/libgd/gd.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,18 @@ void gdImageEllipse(gdImagePtr im, int mx, int my, int w, int h, int c)
17231723
b=h>>1;
17241724
gdImageSetPixel(im,mx+a, my, c);
17251725
gdImageSetPixel(im,mx-a, my, c);
1726+
1727+
if (a <= 0 || b <= 0) {
1728+
return;
1729+
}
1730+
1731+
if (a > (INT64_MAX >> 1) / b / b) {
1732+
return;
1733+
}
1734+
1735+
if (b > (INT64_MAX >> 1) / a / a) {
1736+
return;
1737+
}
17261738
mx1 = mx-a;my1 = my;
17271739
mx2 = mx+a;my2 = my;
17281740

@@ -1762,6 +1774,17 @@ void gdImageFilledEllipse (gdImagePtr im, int mx, int my, int w, int h, int c)
17621774

17631775
a=w>>1;
17641776
b=h>>1;
1777+
if (a <= 0 || b <= 0) {
1778+
return;
1779+
}
1780+
1781+
if (a > (INT64_MAX >> 1) / b / b) {
1782+
return;
1783+
}
1784+
1785+
if (b > (INT64_MAX >> 1) / a / a) {
1786+
return;
1787+
}
17651788

17661789
for (x = mx-a; x <= mx+a; x++) {
17671790
gdImageSetPixel(im, x, my, c);

ext/gd/tests/gh20660-2.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
GH-20660 (imagefilleellipse() overflow)
3+
--EXTENSIONS--
4+
gd
5+
--SKIPIF--
6+
<?php if (getenv("SKIP_SLOW_TESTS")) die("skip slow test"); ?>
7+
--FILE--
8+
<?php
9+
$im = imagecreate(8, 8);
10+
imageellipse($im, 255, 255, "1234567890", 64, 0);
11+
imagefilledellipse($im, 255, 255, "1234567890", 64, 0);
12+
echo "OK";
13+
?>
14+
--EXPECT--
15+
OK

ext/gd/tests/gh20660.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
GH-20660 (imageellipse() overflow)
3+
--EXTENSIONS--
4+
gd
5+
--SKIPIF--
6+
<?php if (getenv("SKIP_SLOW_TESTS")) die("skip slow test"); ?>
7+
--FILE--
8+
<?php
9+
$im = imagecreate(8, 8);
10+
imageellipse($im, 255, 255, "1234567890", 64, 0);
11+
echo "OK";
12+
?>
13+
--EXPECT--
14+
OK

0 commit comments

Comments
 (0)