Skip to content

Commit 6627f78

Browse files
committed
Merge branch 'PHP-7.3' into PHP-7.4
* PHP-7.3: Fix #78620: Out of memory error
2 parents 3ded328 + cf183a5 commit 6627f78

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 #78614 (Does not compile with DTRACE anymore).
77
(tz at FreeBSD dot org)
8+
. Fixed bug #78620 (Out of memory error). (cmb, Nikita)
89

910
- Pcntl:
1011
. Fixed bug #77335 (PHP is preventing SIGALRM from specifying SA_RESTART).

Zend/zend_alloc.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,12 +1754,17 @@ static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_D
17541754
* We allocate them with 2MB size granularity, to avoid many
17551755
* reallocations when they are extended by small pieces
17561756
*/
1757-
size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE));
1757+
size_t alignment = MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE);
17581758
#else
1759-
size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, REAL_PAGE_SIZE);
1759+
size_t alignment = REAL_PAGE_SIZE;
17601760
#endif
1761+
size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, alignment);
17611762
void *ptr;
17621763

1764+
if (UNEXPECTED(new_size < size)) {
1765+
zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu + %zu)", size, alignment);
1766+
}
1767+
17631768
#if ZEND_MM_LIMIT
17641769
if (UNEXPECTED(new_size > heap->limit - heap->real_size)) {
17651770
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)