diff --git a/main/main.c b/main/main.c index 4075be8b377e3..ecc67074cc4c8 100644 --- a/main/main.c +++ b/main/main.c @@ -320,8 +320,35 @@ static PHP_INI_MH(OnChangeMemoryLimit) if (new_value) { value = zend_ini_parse_uquantity_warn(new_value, entry->name); } else { - value = Z_L(1)<<30; /* effectively, no limit */ + value = Z_L(1) << 30; /* effectively, no limit */ } + + /* If max_memory_limit is not set to unlimited, verify change */ + if (PG(max_memory_limit) != -1) { + if (value == -1) { + zend_error( + E_WARNING, + "Failed to set memory_limit to unlimited. memory_limit (currently: " ZEND_LONG_FMT " bytes) cannot be set to unlimited if max_memory_limit (" ZEND_LONG_FMT " bytes) is not unlimited", + PG(memory_limit), + PG(max_memory_limit) + ); + + return FAILURE; + } + + if (value > PG(max_memory_limit)) { + zend_error( + E_WARNING, + "Failed to set memory_limit to %zd bytes. memory_limit (currently: " ZEND_LONG_FMT " bytes) cannot exceed max_memory_limit (" ZEND_LONG_FMT " bytes)", + value, + PG(memory_limit), + PG(max_memory_limit) + ); + + 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. @@ -337,6 +364,26 @@ static PHP_INI_MH(OnChangeMemoryLimit) } /* }}} */ +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 (zend_set_memory_limit(value) == FAILURE) { + zend_error(E_ERROR, "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; + PG(max_memory_limit) = value; + + return SUCCESS; +} + /* {{{ PHP_INI_MH */ static PHP_INI_MH(OnSetLogFilter) { @@ -800,7 +847,10 @@ PHP_INI_BEGIN() STD_PHP_INI_BOOLEAN("mail.mixed_lf_and_crlf", "0", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateBool, mail_mixed_lf_and_crlf, php_core_globals, core_globals) STD_PHP_INI_ENTRY("mail.log", NULL, PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateMailLog, mail_log, php_core_globals, core_globals) PHP_INI_ENTRY("browscap", NULL, PHP_INI_SYSTEM, OnChangeBrowscap) - PHP_INI_ENTRY("memory_limit", "128M", PHP_INI_ALL, OnChangeMemoryLimit) + + PHP_INI_ENTRY("max_memory_limit", "-1", PHP_INI_SYSTEM, OnChangeMaxMemoryLimit) + PHP_INI_ENTRY("memory_limit", "128M", PHP_INI_ALL, OnChangeMemoryLimit) + PHP_INI_ENTRY("precision", "14", PHP_INI_ALL, OnSetPrecision) PHP_INI_ENTRY("sendmail_from", NULL, PHP_INI_ALL, NULL) PHP_INI_ENTRY("sendmail_path", DEFAULT_SENDMAIL_PATH, PHP_INI_SYSTEM, NULL) diff --git a/main/php_globals.h b/main/php_globals.h index b2f2696c2db2c..59ad55ffff4c8 100644 --- a/main/php_globals.h +++ b/main/php_globals.h @@ -72,6 +72,7 @@ struct _php_core_globals { zend_long serialize_precision; zend_long memory_limit; + zend_long max_memory_limit; zend_long max_input_time; char *error_log; diff --git a/php.ini-development b/php.ini-development index 37619fd071c55..80ac80557f1b2 100644 --- a/php.ini-development +++ b/php.ini-development @@ -436,6 +436,7 @@ max_input_time = 60 ; Maximum amount of memory a script may consume ; https://php.net/memory-limit memory_limit = 128M +max_memory_limit = -1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Error handling and logging ; diff --git a/php.ini-production b/php.ini-production index 60069f69b8b90..a5b70aed6c976 100644 --- a/php.ini-production +++ b/php.ini-production @@ -438,6 +438,7 @@ max_input_time = 60 ; Maximum amount of memory a script may consume ; https://php.net/memory-limit memory_limit = 128M +max_memory_limit = -1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Error handling and logging ; diff --git a/tests/basic/gh17951_ini_parse_1.phpt b/tests/basic/gh17951_ini_parse_1.phpt new file mode 100644 index 0000000000000..621c4984648f3 --- /dev/null +++ b/tests/basic/gh17951_ini_parse_1.phpt @@ -0,0 +1,15 @@ +--TEST-- +GH-17951 INI Parse 1 +--CREDITS-- +Frederik Milling Pytlick +frederikpyt@protonmail.com +--INI-- +memory_limit=128M +max_memory_limit=-1 +--FILE-- +