Skip to content

Commit abaf9a7

Browse files
committed
Fix #78620: Out of memory error
The integer addition in `ZEND_MM_ALIGNED_SIZE_EX` can overflow, what we have to catch early.
1 parent a6d2196 commit abaf9a7

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ PHP NEWS
55
- Core:
66
. Fixed bug #78535 (auto_detect_line_endings value not parsed as bool).
77
(bugreportuser)
8+
. Fixed bug #78620 (Out of memory error). (cmb, Nikita)
89

910
- Exif:
1011
. Fixed bug #78442 ('Illegal component' on exif_read_data since PHP7)

Zend/zend_alloc.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,12 +1723,17 @@ static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_D
17231723
* We allocate them with 2MB size granularity, to avoid many
17241724
* reallocations when they are extended by small pieces
17251725
*/
1726-
size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE));
1726+
size_t alignment = MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE);
17271727
#else
1728-
size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, REAL_PAGE_SIZE);
1728+
size_t alignment = REAL_PAGE_SIZE;
17291729
#endif
1730+
size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, alignment);
17301731
void *ptr;
17311732

1733+
if (UNEXPECTED(new_size < size)) {
1734+
zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu + %zu)", size, alignment);
1735+
}
1736+
17321737
#if ZEND_MM_LIMIT
17331738
if (UNEXPECTED(new_size > heap->limit - heap->real_size)) {
17341739
if (zend_mm_gc(heap) && new_size <= heap->limit - heap->real_size) {

ext/standard/tests/strings/wordwrap_memory_limit.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
No overflow should occur during the memory_limit check for wordwrap()
33
--SKIPIF--
44
<?php
5+
if (substr(PHP_OS, 0, 3) == 'WIN' && PHP_INT_SIZE == 4) die("skip this test is not for 32bit Windows platforms");
56
if (getenv("USE_ZEND_ALLOC") === "0") die("skip Zend MM disabled");
67
?>
78
--INI--
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
No overflow should occur during the memory_limit check for wordwrap()
3+
--SKIPIF--
4+
<?php
5+
if (substr(PHP_OS, 0, 3) != 'WIN' || PHP_INT_SIZE != 4) die("skip this test is for 32bit Windows platforms only");
6+
if (getenv("USE_ZEND_ALLOC") === "0") die("skip Zend MM disabled");
7+
?>
8+
--INI--
9+
memory_limit=128M
10+
--FILE--
11+
<?php
12+
13+
$str = str_repeat('x', 65534);
14+
$str2 = str_repeat('x', 65535);
15+
wordwrap($str, 1, $str2);
16+
17+
?>
18+
--EXPECTF--
19+
Fatal error: Possible integer overflow in memory allocation (4294901777 + %d) in %s on line %d

0 commit comments

Comments
 (0)