Skip to content

Commit dfd8237

Browse files
cmb69smalyshev
authored andcommitted
Fix #77269: Potential unsigned underflow in gdImageScale
Belatedly, we're porting the respective upstream patch[1]. [1] <libgd/libgd@60bfb40>
1 parent 78bd347 commit dfd8237

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

ext/gd/libgd/gd_interpolation.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -890,8 +890,13 @@ static inline LineContribType * _gdContributionsAlloc(unsigned int line_length,
890890
{
891891
unsigned int u = 0;
892892
LineContribType *res;
893-
int overflow_error = 0;
893+
size_t weights_size;
894894

895+
if (overflow2(windows_size, sizeof(double))) {
896+
return NULL;
897+
} else {
898+
weights_size = windows_size * sizeof(double);
899+
}
895900
res = (LineContribType *) gdMalloc(sizeof(LineContribType));
896901
if (!res) {
897902
return NULL;
@@ -908,15 +913,10 @@ static inline LineContribType * _gdContributionsAlloc(unsigned int line_length,
908913
return NULL;
909914
}
910915
for (u = 0 ; u < line_length ; u++) {
911-
if (overflow2(windows_size, sizeof(double))) {
912-
overflow_error = 1;
913-
} else {
914-
res->ContribRow[u].Weights = (double *) gdMalloc(windows_size * sizeof(double));
915-
}
916-
if (overflow_error == 1 || res->ContribRow[u].Weights == NULL) {
916+
res->ContribRow[u].Weights = (double *) gdMalloc(weights_size);
917+
if (res->ContribRow[u].Weights == NULL) {
917918
unsigned int i;
918-
u--;
919-
for (i=0;i<=u;i++) {
919+
for (i=0;i<u;i++) {
920920
gdFree(res->ContribRow[i].Weights);
921921
}
922922
gdFree(res->ContribRow);

ext/gd/tests/bug77269.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Bug #77269 (Potential unsigned underflow in gdImageScale)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gd')) die('skip gd extension not available');
6+
if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
7+
?>
8+
--INI--
9+
memory_limit=2G
10+
--FILE--
11+
<?php
12+
$im = imagecreate(2**28, 1);
13+
if(is_resource($im)) {
14+
imagescale($im, 1, 1, IMG_TRIANGLE);
15+
}
16+
?>
17+
===DONE===
18+
--EXPECTF--
19+
Warning: imagecreate():%S product of memory allocation multiplication would exceed INT_MAX, failing operation gracefully
20+
in %s on line %d
21+
===DONE===

0 commit comments

Comments
 (0)