Skip to content

Commit 5dd3909

Browse files
committed
Fix GH-19739: imageellipse/imagefilledellipse overflow.
Port the upstream libgd portable fix (overflowMul3) for the int64 overflow at r = a * bq when w/h reach near INT_MAX. Mirrors the upstream libgd commit 0057de6. close GH-22116
1 parent ec8342c commit 5dd3909

5 files changed

Lines changed: 45 additions & 1 deletion

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ PHP NEWS
77
overflow-triggered early return). (iliaal)
88
. Fixed bug GH-19666 (imageconvolution() unexpected nan filter value).
99
(David Carlier)
10+
. Fixed bug GH-19739 (imageellipse/imagefilledellipse overflow).
11+
(David Carlier)
1012

1113
- Intl:
1214
. Fix incorrect argument positions for uninitialized calendar arguments in

ext/gd/libgd/gd.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,9 @@ void gdImageEllipse(gdImagePtr im, int mx, int my, int w, int h, int c)
17751775

17761776
a=w>>1;
17771777
b=h>>1;
1778+
if (overflowMul3(a, b, b) || overflowMul3(b, a, a)) {
1779+
return;
1780+
}
17781781
gdImageSetPixel(im,mx+a, my, c);
17791782
gdImageSetPixel(im,mx-a, my, c);
17801783
mx1 = mx-a;my1 = my;
@@ -1816,7 +1819,9 @@ void gdImageFilledEllipse (gdImagePtr im, int mx, int my, int w, int h, int c)
18161819

18171820
a=w>>1;
18181821
b=h>>1;
1819-
1822+
if (overflowMul3(a, b, b) || overflowMul3(b, a, a)) {
1823+
return;
1824+
}
18201825
for (x = mx-a; x <= mx+a; x++) {
18211826
gdImageSetPixel(im, x, my, c);
18221827
}

ext/gd/libgd/gd_security.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <stdio.h>
1616
#include <stdlib.h>
17+
#include <stdint.h>
1718
#include <limits.h>
1819
#include "gd.h"
1920
#include "gd_errors.h"
@@ -30,3 +31,20 @@ int overflow2(int a, int b)
3031
}
3132
return 0;
3233
}
34+
35+
int overflowMul3(int a, int b, int c)
36+
{
37+
if (a < 0 || b < 0 || c < 0) {
38+
return 1;
39+
}
40+
if (a == 0 || b == 0 || c == 0) {
41+
return 0;
42+
}
43+
if (a > INT_MAX / b) {
44+
return 1;
45+
}
46+
if ((int64_t)a * b > INT64_MAX / c) {
47+
return 1;
48+
}
49+
return 0;
50+
}

ext/gd/libgd/gdhelpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extern char *gd_strtok_r(char *s, char *sep, char **state);
2727
netpbm fixes by Alan Cox. */
2828

2929
int overflow2(int a, int b);
30+
int overflowMul3(int a, int b, int c);
3031

3132
#ifdef ZTS
3233
#define gdMutexDeclare(x) MUTEX_T x

ext/gd/tests/gh19739.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
GH-19739 (integer overflow in imageellipse / imagefilledellipse)
3+
--EXTENSIONS--
4+
gd
5+
--FILE--
6+
<?php
7+
$im = imagecreatetruecolor(400, 300);
8+
$color = imagecolorallocate($im, 150, 255, 0);
9+
10+
var_dump(imageellipse($im, 64, 150, 2147483647, 2147483647, $color));
11+
var_dump(imagefilledellipse($im, 64, 150, 2147483647, 2147483647, $color));
12+
13+
echo "done" . PHP_EOL;
14+
?>
15+
--EXPECT--
16+
bool(true)
17+
bool(true)
18+
done

0 commit comments

Comments
 (0)