Skip to content

Commit f801259

Browse files
committed
#72337 invalid dimensions can lead to segv
1 parent 2a0ed8a commit f801259

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

ext/gd/gd.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5145,6 +5145,10 @@ PHP_FUNCTION(imagescale)
51455145
}
51465146
}
51475147

5148+
if (tmp_h <= 0 || tmp_w <= 0) {
5149+
RETURN_FALSE;
5150+
}
5151+
51485152
new_width = tmp_w;
51495153
new_height = tmp_h;
51505154

ext/gd/libgd/gd_interpolation.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,10 @@ gdImagePtr gdImageScaleTwoPass(const gdImagePtr src, const unsigned int src_widt
10591059
gdImagePtr tmp_im;
10601060
gdImagePtr dst;
10611061

1062+
if (new_width == 0 || new_height == 0) {
1063+
return NULL;
1064+
}
1065+
10621066
/* Convert to truecolor if it isn't; this code requires it. */
10631067
if (!src->trueColor) {
10641068
gdImagePaletteToTrueColor(src);
@@ -1087,6 +1091,10 @@ gdImagePtr Scale(const gdImagePtr src, const unsigned int src_width, const unsig
10871091
{
10881092
gdImagePtr tmp_im;
10891093

1094+
if (new_width == 0 || new_height == 0) {
1095+
return NULL;
1096+
}
1097+
10901098
tmp_im = gdImageCreateTrueColor(new_width, src_height);
10911099
if (tmp_im == NULL) {
10921100
return NULL;
@@ -1120,6 +1128,10 @@ gdImagePtr gdImageScaleNearestNeighbour(gdImagePtr im, const unsigned int width,
11201128
unsigned long dst_offset_y = 0;
11211129
unsigned int i;
11221130

1131+
if (new_width == 0 || new_height == 0) {
1132+
return NULL;
1133+
}
1134+
11231135
dst_img = gdImageCreateTrueColor(new_width, new_height);
11241136

11251137
if (dst_img == NULL) {
@@ -1221,6 +1233,10 @@ static gdImagePtr gdImageScaleBilinearPalette(gdImagePtr im, const unsigned int
12211233
gdImagePtr new_img;
12221234
const int transparent = im->transparent;
12231235

1236+
if (new_width == 0 || new_height == 0) {
1237+
return NULL;
1238+
}
1239+
12241240
new_img = gdImageCreateTrueColor(new_width, new_height);
12251241
if (new_img == NULL) {
12261242
return NULL;
@@ -1313,6 +1329,10 @@ static gdImagePtr gdImageScaleBilinearTC(gdImagePtr im, const unsigned int new_w
13131329
long i;
13141330
gdImagePtr new_img;
13151331

1332+
if (new_width == 0 || new_height == 0) {
1333+
return NULL;
1334+
}
1335+
13161336
new_img = gdImageCreateTrueColor(new_width, new_height);
13171337
if (!new_img){
13181338
return NULL;
@@ -1412,6 +1432,10 @@ gdImagePtr gdImageScaleBicubicFixed(gdImagePtr src, const unsigned int width, co
14121432
unsigned int dst_offset_y = 0;
14131433
long i;
14141434

1435+
if (new_width == 0 || new_height == 0) {
1436+
return NULL;
1437+
}
1438+
14151439
/* impact perf a bit, but not that much. Implementation for palette
14161440
images can be done at a later point.
14171441
*/
@@ -1634,7 +1658,11 @@ gdImagePtr gdImageScale(const gdImagePtr src, const unsigned int new_width, cons
16341658
gdImagePtr im_scaled = NULL;
16351659

16361660
if (src == NULL || src->interpolation_id < 0 || src->interpolation_id > GD_METHOD_COUNT) {
1637-
return 0;
1661+
return NULL;
1662+
}
1663+
1664+
if (new_width == 0 || new_height == 0) {
1665+
return NULL;
16381666
}
16391667

16401668
switch (src->interpolation_id) {
@@ -1680,6 +1708,10 @@ gdImagePtr gdImageRotateNearestNeighbour(gdImagePtr src, const float degrees, co
16801708
unsigned int i;
16811709
gdImagePtr dst;
16821710

1711+
if (new_width == 0 || new_height == 0) {
1712+
return NULL;
1713+
}
1714+
16831715
dst = gdImageCreateTrueColor(new_width, new_height);
16841716
if (!dst) {
16851717
return NULL;

ext/gd/tests/bug72337.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
#72337 segfault in imagescale with new dimensions being <=0)
3+
--SKIPIF--
4+
<?php
5+
if (!function_exists('imagescale')) die("skip gd extension not available\n");
6+
?>
7+
--FILE--
8+
<?php
9+
$im = imagecreatetruecolor(1, 1);
10+
imagescale($im, 0, 0, IMG_BICUBIC_FIXED);
11+
echo "OK";
12+
?>
13+
--EXPECT--
14+
OK

0 commit comments

Comments
 (0)