Skip to content

Commit a918020

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 428d816 commit a918020

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
@@ -880,8 +880,13 @@ static inline LineContribType * _gdContributionsAlloc(unsigned int line_length,
880880
{
881881
unsigned int u = 0;
882882
LineContribType *res;
883-
int overflow_error = 0;
883+
size_t weights_size;
884884

885+
if (overflow2(windows_size, sizeof(double))) {
886+
return NULL;
887+
} else {
888+
weights_size = windows_size * sizeof(double);
889+
}
885890
res = (LineContribType *) gdMalloc(sizeof(LineContribType));
886891
if (!res) {
887892
return NULL;
@@ -898,15 +903,10 @@ static inline LineContribType * _gdContributionsAlloc(unsigned int line_length,
898903
return NULL;
899904
}
900905
for (u = 0 ; u < line_length ; u++) {
901-
if (overflow2(windows_size, sizeof(double))) {
902-
overflow_error = 1;
903-
} else {
904-
res->ContribRow[u].Weights = (double *) gdMalloc(windows_size * sizeof(double));
905-
}
906-
if (overflow_error == 1 || res->ContribRow[u].Weights == NULL) {
906+
res->ContribRow[u].Weights = (double *) gdMalloc(weights_size);
907+
if (res->ContribRow[u].Weights == NULL) {
907908
unsigned int i;
908-
u--;
909-
for (i=0;i<=u;i++) {
909+
for (i=0;i<u;i++) {
910910
gdFree(res->ContribRow[i].Weights);
911911
}
912912
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)