Skip to content

Commit 68d01d2

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: Fix GH-19739: imageellipse/imagefilledellipse overflow.
2 parents 973d511 + 4659762 commit 68d01d2

4 files changed

Lines changed: 43 additions & 1 deletion

File tree

ext/gd/libgd/gd.c

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

17841784
a=w>>1;
17851785
b=h>>1;
1786+
if (overflowMul3(a, b, b) || overflowMul3(b, a, a)) {
1787+
return;
1788+
}
17861789
gdImageSetPixel(im,mx+a, my, c);
17871790
gdImageSetPixel(im,mx-a, my, c);
17881791
mx1 = mx-a;my1 = my;
@@ -1824,7 +1827,9 @@ void gdImageFilledEllipse (gdImagePtr im, int mx, int my, int w, int h, int c)
18241827

18251828
a=w>>1;
18261829
b=h>>1;
1827-
1830+
if (overflowMul3(a, b, b) || overflowMul3(b, a, a)) {
1831+
return;
1832+
}
18281833
for (x = mx-a; x <= mx+a; x++) {
18291834
gdImageSetPixel(im, x, my, c);
18301835
}

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)