Skip to content

Commit bad49e5

Browse files
committed
Merge branch 'PHP-7.3' into PHP-7.4
2 parents c810d3d + a26f632 commit bad49e5

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

Zend/zend_alloc.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ static void *zend_mm_alloc_pages(zend_mm_heap *heap, uint32_t pages_count ZEND_F
939939
heap->cached_chunks = chunk->next;
940940
} else {
941941
#if ZEND_MM_LIMIT
942-
if (UNEXPECTED(heap->real_size + ZEND_MM_CHUNK_SIZE > heap->limit)) {
942+
if (UNEXPECTED(ZEND_MM_CHUNK_SIZE > heap->limit - heap->real_size)) {
943943
if (zend_mm_gc(heap)) {
944944
goto get_chunk;
945945
} else if (heap->overflow == 0) {
@@ -1465,8 +1465,8 @@ static zend_never_inline void *zend_mm_realloc_huge(zend_mm_heap *heap, void *pt
14651465
}
14661466
} else /* if (new_size > old_size) */ {
14671467
#if ZEND_MM_LIMIT
1468-
if (UNEXPECTED(heap->real_size + (new_size - old_size) > heap->limit)) {
1469-
if (zend_mm_gc(heap) && heap->real_size + (new_size - old_size) <= heap->limit) {
1468+
if (UNEXPECTED(new_size - old_size > heap->limit - heap->real_size)) {
1469+
if (zend_mm_gc(heap) && new_size - old_size <= heap->limit - heap->real_size) {
14701470
/* pass */
14711471
} else if (heap->overflow == 0) {
14721472
#if ZEND_DEBUG
@@ -1752,8 +1752,8 @@ static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_D
17521752
void *ptr;
17531753

17541754
#if ZEND_MM_LIMIT
1755-
if (UNEXPECTED(heap->real_size + new_size > heap->limit)) {
1756-
if (zend_mm_gc(heap) && heap->real_size + new_size <= heap->limit) {
1755+
if (UNEXPECTED(new_size > heap->limit - heap->real_size)) {
1756+
if (zend_mm_gc(heap) && new_size <= heap->limit - heap->real_size) {
17571757
/* pass */
17581758
} else if (heap->overflow == 0) {
17591759
#if ZEND_DEBUG
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
No overflow should occur during the memory_limit check for wordwrap()
3+
--SKIPIF--
4+
<?php
5+
if (getenv("USE_ZEND_ALLOC") === "0") die("skip Zend MM disabled");
6+
?>
7+
--INI--
8+
memory_limit=128M
9+
--FILE--
10+
<?php
11+
12+
$str = str_repeat('x', 65534);
13+
$str2 = str_repeat('x', 65535);
14+
wordwrap($str, 1, $str2);
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Allowed memory size of 134217728 bytes exhausted%s(tried to allocate %d bytes) in %s on line %d

0 commit comments

Comments
 (0)