Skip to content

Fix GH-17951: Addition of max_memory_limit INI #18011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Fix issues addressed during review
  • Loading branch information
frederikpyt committed Mar 10, 2025
commit da70576536475ca365467a2c1c87db216f7ac8f6
44 changes: 24 additions & 20 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,11 @@ static PHP_INI_MH(OnChangeMemoryLimit)
{
size_t value;

if (new_value) {
value = zend_ini_parse_uquantity_warn(new_value, entry->name);
} else {
value = Z_L(1) << 30; /* effectively, no limit */
}
if (new_value) {
value = zend_ini_parse_uquantity_warn(new_value, entry->name);
} else {
value = Z_L(1) << 30; /* effectively, no limit */
}

/* If max_memory_limit is not set to unlimited, verify change */
if (PG(max_memory_limit) != 0 && PG(max_memory_limit) != -1) {
Expand Down Expand Up @@ -350,27 +350,31 @@ static PHP_INI_MH(OnChangeMemoryLimit)
}
}

if (zend_set_memory_limit(value) == FAILURE) {
if (stage != ZEND_INI_STAGE_DEACTIVATE) {
zend_error(E_WARNING, "Failed to set memory limit to %zd bytes (Current memory usage is %zd bytes)", value, zend_memory_usage(true));
return FAILURE;
}
}
if (zend_set_memory_limit(value) == FAILURE) {
/* When the memory limit is reset to the original level during deactivation, we may be
* using more memory than the original limit while shutdown is still in progress.
* Ignore a failure for now, and set the memory limit when the memory manager has been
* shut down and the minimal amount of memory is used. */
if (stage != ZEND_INI_STAGE_DEACTIVATE) {
zend_error(E_WARNING, "Failed to set memory limit to %zd bytes (Current memory usage is %zd bytes)", value, zend_memory_usage(true));
return FAILURE;
}
}

PG(memory_limit) = value;
return SUCCESS;
PG(memory_limit) = value;
return SUCCESS;
}
/* }}} */

/* {{{ PHP_INI_MH */
static PHP_INI_MH(OnChangeMaxMemoryLimit)
{
size_t value;
if (new_value) {
value = zend_ini_parse_uquantity_warn(new_value, entry->name);
} else {
value = Z_L(1) << 30; /* effectively, no limit */
}
if (new_value) {
value = zend_ini_parse_uquantity_warn(new_value, entry->name);
} else {
value = Z_L(1) << 30; /* effectively, no limit */
}

/* If new value is not unlimited, verify change */
if (value != -1) {
Expand All @@ -394,8 +398,8 @@ static PHP_INI_MH(OnChangeMaxMemoryLimit)
}
}

PG(max_memory_limit) = value;
return SUCCESS;
PG(max_memory_limit) = value;
return SUCCESS;
}
/* }}} */

Expand Down
2 changes: 1 addition & 1 deletion main/php_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct _php_core_globals {
zend_long serialize_precision;

zend_long memory_limit;
zend_long max_memory_limit;
zend_long max_memory_limit;
zend_long max_input_time;

char *error_log;
Expand Down
Loading