Skip to content

Commit b4559e7

Browse files
committed
Merge branch 'PHP-8.0'
* PHP-8.0: Fixed bug #81070
2 parents 477f4fe + 324ad2f commit b4559e7

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

Zend/tests/bug81070.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Bug #81070 Setting memory limit to below current usage
3+
--FILE--
4+
<?php
5+
$a = str_repeat("0", 5 * 1024 * 1024);
6+
ini_set("memory_limit", "3M");
7+
?>
8+
--EXPECTF--
9+
Warning: Failed to set memory limit to 3145728 bytes (Current memory usage is %d bytes) in %s on line %d

Zend/zend_alloc.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2658,11 +2658,24 @@ ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length)
26582658
return p;
26592659
}
26602660

2661+
ZEND_API zend_result zend_set_memory_limit_ex(size_t memory_limit)
2662+
{
2663+
#if ZEND_MM_LIMIT
2664+
if (memory_limit < ZEND_MM_CHUNK_SIZE) {
2665+
memory_limit = ZEND_MM_CHUNK_SIZE;
2666+
}
2667+
if (UNEXPECTED(memory_limit < AG(mm_heap)->real_size)) {
2668+
return FAILURE;
2669+
}
2670+
AG(mm_heap)->limit = memory_limit;
2671+
#endif
2672+
return SUCCESS;
2673+
}
26612674

26622675
ZEND_API void zend_set_memory_limit(size_t memory_limit)
26632676
{
26642677
#if ZEND_MM_LIMIT
2665-
AG(mm_heap)->limit = (memory_limit >= ZEND_MM_CHUNK_SIZE) ? memory_limit : ZEND_MM_CHUNK_SIZE;
2678+
AG(mm_heap)->limit = memory_limit >= ZEND_MM_CHUNK_SIZE ? memory_limit : ZEND_MM_CHUNK_SIZE;
26662679
#endif
26672680
}
26682681

Zend/zend_alloc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ ZEND_API void * __zend_realloc(void *p, size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE(2)
220220
#define pestrdup_rel(s, persistent) ((persistent)?strdup(s):estrdup_rel(s))
221221

222222
ZEND_API void zend_set_memory_limit(size_t memory_limit);
223+
ZEND_API zend_result zend_set_memory_limit_ex(size_t memory_limit);
223224

224225
ZEND_API void start_memory_manager(void);
225226
ZEND_API void shutdown_memory_manager(bool silent, bool full_shutdown);

main/main.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,17 @@ static PHP_INI_MH(OnSetSerializePrecision)
266266
/* {{{ PHP_INI_MH */
267267
static PHP_INI_MH(OnChangeMemoryLimit)
268268
{
269+
size_t value;
269270
if (new_value) {
270-
PG(memory_limit) = zend_atol(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
271+
value = zend_atol(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
271272
} else {
272-
PG(memory_limit) = Z_L(1)<<30; /* effectively, no limit */
273+
value = Z_L(1)<<30; /* effectively, no limit */
273274
}
274-
zend_set_memory_limit(PG(memory_limit));
275+
if (zend_set_memory_limit_ex(value) == FAILURE) {
276+
zend_error(E_WARNING, "Failed to set memory limit to %zd bytes (Current memory usage is %zd bytes)", value, zend_memory_usage(true));
277+
return FAILURE;
278+
}
279+
PG(memory_limit) = value;
275280
return SUCCESS;
276281
}
277282
/* }}} */

0 commit comments

Comments
 (0)