Skip to content

Commit f5622f5

Browse files
committed
Fix #72709: imagesetstyle() causes OOB read for empty $styles
Calling imagesetstyle() with an empty $styles array caused gdImageSetStyle() to be called with `noOfPixels==0`, what could have lead to OOB reads. Actually, this issue will be fixed in libgd, but to avoid issues when older libgd is in use, we simply disallow passing an empty $styles array to imagesetstyle(), what wouldn't serve a useful purpose anyway.
1 parent e95625f commit f5622f5

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ PHP NEWS
4242
blendingmode). (cmb)
4343
. Fixed bug #66555 (Always false condition in ext/gd/libgd/gdkanji.c). (cmb)
4444
. Fixed bug #68712 (suspicious if-else statements). (cmb)
45+
. Fixed bug #72709 (imagesetstyle() causes OOB read for empty $styles). (cmb)
4546

4647
- Intl:
4748
. Partially fixed #72506 (idn_to_ascii for UTS #46 incorrect for long domain

ext/gd/gd.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1555,15 +1555,22 @@ PHP_FUNCTION(imagesetstyle)
15551555
int * stylearr;
15561556
int index;
15571557
HashPosition pos;
1558+
int num_styles;
15581559

15591560
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &IM, &styles) == FAILURE) {
15601561
return;
15611562
}
15621563

15631564
ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
15641565

1566+
num_styles = zend_hash_num_elements(HASH_OF(styles));
1567+
if (num_styles == 0) {
1568+
php_error_docref(NULL, E_WARNING, "styles array must not be empty");
1569+
RETURN_FALSE;
1570+
}
1571+
15651572
/* copy the style values in the stylearr */
1566-
stylearr = safe_emalloc(sizeof(int), zend_hash_num_elements(HASH_OF(styles)), 0);
1573+
stylearr = safe_emalloc(sizeof(int), num_styles, 0);
15671574

15681575
zend_hash_internal_pointer_reset_ex(HASH_OF(styles), &pos);
15691576

ext/gd/tests/bug72709.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Bug #72709 (imagesetstyle() causes OOB read for empty $styles)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gd')) die('skip ext/gd not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$im = imagecreatetruecolor(1, 1);
10+
var_dump(imagesetstyle($im, array()));
11+
imagesetpixel($im, 0, 0, IMG_COLOR_STYLED);
12+
imagedestroy($im);
13+
?>
14+
====DONE====
15+
--EXPECTF--
16+
Warning: imagesetstyle(): styles array must not be empty in %s%ebug72709.php on line %d
17+
bool(false)
18+
====DONE====

0 commit comments

Comments
 (0)