Skip to content

Commit cf183a5

Browse files
committed
Merge branch 'PHP-7.2' into PHP-7.3
* PHP-7.2: Fix #78620: Out of memory error
2 parents f45eb35 + abaf9a7 commit cf183a5

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
@@ -1792,12 +1792,17 @@ static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_D
17921792
* We allocate them with 2MB size granularity, to avoid many
17931793
* reallocations when they are extended by small pieces
17941794
*/
1795-
size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE));
1795+
size_t alignment = MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE);
17961796
#else
1797-
size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, REAL_PAGE_SIZE);
1797+
size_t alignment = REAL_PAGE_SIZE;
17981798
#endif
1799+
size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, alignment);
17991800
void *ptr;
18001801

1802+
if (UNEXPECTED(new_size < size)) {
1803+
zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu + %zu)", size, alignment);
1804+
}
1805+
18011806
#if ZEND_MM_LIMIT
18021807
if (UNEXPECTED(new_size > heap->limit - heap->real_size)) {
18031808
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)