Skip to content

Commit 6007c09

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: Fix GH-19730: undefined behavior in gd_interpolation.c.
2 parents f357164 + cb8f7bd commit 6007c09

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

ext/gd/libgd/gd_interpolation.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <stdlib.h>
5858
#include <string.h>
5959
#include <math.h>
60+
#include <limits.h>
6061

6162
#include "gd.h"
6263
#include "gdhelpers.h"
@@ -1873,7 +1874,10 @@ int gdTransformAffineGetImage(gdImagePtr *dst,
18731874
src_area = &area_full;
18741875
}
18751876

1876-
gdTransformAffineBoundingBox(src_area, affine, &bbox);
1877+
if (gdTransformAffineBoundingBox(src_area, affine, &bbox) != GD_TRUE) {
1878+
*dst = NULL;
1879+
return GD_FALSE;
1880+
}
18771881

18781882
*dst = gdImageCreateTrueColor(bbox.width, bbox.height);
18791883
if (*dst == NULL) {
@@ -2049,6 +2053,8 @@ int gdTransformAffineCopy(gdImagePtr dst,
20492053
int gdTransformAffineBoundingBox(gdRectPtr src, const double affine[6], gdRectPtr bbox)
20502054
{
20512055
gdPointF extent[4], min, max, point;
2056+
double width, height;
2057+
int bbox_x, bbox_y, bbox_width, bbox_height;
20522058
int i;
20532059

20542060
extent[0].x=0.0;
@@ -2079,10 +2085,29 @@ int gdTransformAffineBoundingBox(gdRectPtr src, const double affine[6], gdRectPt
20792085
if (max.y < extent[i].y)
20802086
max.y=extent[i].y;
20812087
}
2082-
bbox->x = (int) min.x;
2083-
bbox->y = (int) min.y;
2084-
bbox->width = (int) floor(max.x - min.x) - 1;
2085-
bbox->height = (int) floor(max.y - min.y);
2088+
width = floor(max.x - min.x);
2089+
height = floor(max.y - min.y);
2090+
if (!isfinite(min.x) || !isfinite(min.y) || !isfinite(width) || !isfinite(height)
2091+
|| min.x <= INT_MIN || min.x > INT_MAX
2092+
|| min.y <= INT_MIN || min.y > INT_MAX
2093+
|| width < 1.0 || width > INT_MAX
2094+
|| height < 0.0 || height > INT_MAX) {
2095+
return GD_FALSE;
2096+
}
2097+
bbox_x = (int) min.x;
2098+
bbox_y = (int) min.y;
2099+
bbox_width = (int) width - 1;
2100+
bbox_height = (int) height;
2101+
if ((bbox_x < 0 && bbox_width > INT_MAX + bbox_x)
2102+
|| (bbox_x > 0 && bbox_width > INT_MAX - bbox_x)
2103+
|| (bbox_y < 0 && bbox_height > INT_MAX + bbox_y)
2104+
|| (bbox_y > 0 && bbox_height > INT_MAX - bbox_y)) {
2105+
return GD_FALSE;
2106+
}
2107+
bbox->x = bbox_x;
2108+
bbox->y = bbox_y;
2109+
bbox->width = bbox_width;
2110+
bbox->height = bbox_height;
20862111
return GD_TRUE;
20872112
}
20882113

ext/gd/tests/gh19730.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
GH-19730 (undefined behavior in gd_interpolation.c)
3+
--EXTENSIONS--
4+
gd
5+
--SKIPIF--
6+
<?php
7+
if (!GD_BUNDLED) {
8+
die("skip meaningful only for bundled libgd\n");
9+
}
10+
?>
11+
--FILE--
12+
<?php
13+
$im = imagecreatetruecolor(8, 8);
14+
$affine = [1211472000, 1, 1, 1, 1, 1];
15+
var_dump(imageaffine($im, $affine));
16+
?>
17+
--EXPECT--
18+
bool(false)

0 commit comments

Comments
 (0)