Download | Plain Text | No Line Numbers


  1. diff -Naur a/main/main.c b/main/main.c
  2. --- a/main/main.c 2025-09-19 10:08:21.892371864 +0200
  3. +++ b/main/main.c 2025-09-19 10:08:48.159196785 +0200
  4. @@ -278,6 +278,16 @@
  5. } else {
  6. value = Z_L(1)<<30; /* effectively, no limit */
  7. }
  8. +
  9. + /* If memory_limit exceeds max_memory_limit, set to max_memory_limit instead. */
  10. + if (value > PG(max_memory_limit)) {
  11. + zend_ini_entry *max_mem_limit_ini = zend_hash_str_find_ptr(EG(ini_directives), ZEND_STRL("max_memory_limit"));
  12. + entry->value = zend_string_init(ZSTR_VAL(max_mem_limit_ini->value), ZSTR_LEN(max_mem_limit_ini->value), true);
  13. + PG(memory_limit) = PG(max_memory_limit);
  14. +
  15. + return SUCCESS;
  16. + }
  17. +
  18. if (zend_set_memory_limit(value) == FAILURE) {
  19. /* When the memory limit is reset to the original level during deactivation, we may be
  20. * using more memory than the original limit while shutdown is still in progress.
  21. @@ -293,6 +303,26 @@
  22. }
  23. /* }}} */
  24.  
  25. +static PHP_INI_MH(OnChangeMaxMemoryLimit)
  26. +{
  27. + size_t value;
  28. + if (new_value) {
  29. + value = zend_ini_parse_uquantity_warn(new_value, entry->name);
  30. + } else {
  31. + value = Z_L(1) << 30; /* effectively, no limit */
  32. + }
  33. +
  34. + if (zend_set_memory_limit(value) == FAILURE) {
  35. + zend_error(E_ERROR, "Failed to set memory limit to %zd bytes (Current memory usage is %zd bytes)", value, zend_memory_usage(true));
  36. + return FAILURE;
  37. + }
  38. +
  39. + PG(max_memory_limit) = value;
  40. + zend_alter_ini_entry(ZSTR_INIT_LITERAL("memory_limit", 1), new_value, PHP_INI_ALL, stage);
  41. +
  42. + return SUCCESS;
  43. +}
  44. +
  45. /* {{{ PHP_INI_MH */
  46. static PHP_INI_MH(OnSetLogFilter)
  47. {
  48. @@ -751,7 +781,10 @@
  49. 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)
  50. STD_PHP_INI_ENTRY("mail.log", NULL, PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateMailLog, mail_log, php_core_globals, core_globals)
  51. PHP_INI_ENTRY("browscap", NULL, PHP_INI_SYSTEM, OnChangeBrowscap)
  52. - PHP_INI_ENTRY("memory_limit", "128M", PHP_INI_ALL, OnChangeMemoryLimit)
  53. +
  54. + PHP_INI_ENTRY("max_memory_limit", "-1", PHP_INI_SYSTEM, OnChangeMaxMemoryLimit)
  55. + PHP_INI_ENTRY("memory_limit", "128M", PHP_INI_ALL, OnChangeMemoryLimit)
  56. +
  57. PHP_INI_ENTRY("precision", "14", PHP_INI_ALL, OnSetPrecision)
  58. PHP_INI_ENTRY("sendmail_from", NULL, PHP_INI_ALL, NULL)
  59. PHP_INI_ENTRY("sendmail_path", DEFAULT_SENDMAIL_PATH, PHP_INI_SYSTEM, NULL)
  60. diff -Naur a/main/php_globals.h b/main/php_globals.h
  61. --- a/main/php_globals.h 2025-09-19 10:08:21.745372843 +0200
  62. +++ b/main/php_globals.h 2025-09-19 10:08:48.159196785 +0200
  63. @@ -72,6 +72,7 @@
  64. zend_long serialize_precision;
  65.  
  66. zend_long memory_limit;
  67. + zend_long max_memory_limit;
  68. zend_long max_input_time;
  69.  
  70. char *error_log;
  71. diff -Naur a/php.ini-development b/php.ini-development
  72. --- a/php.ini-development 2025-09-19 10:08:21.746372837 +0200
  73. +++ b/php.ini-development 2025-09-19 10:08:48.160196778 +0200
  74. @@ -445,6 +445,7 @@
  75. ; Maximum amount of memory a script may consume
  76. ; https://php.net/memory-limit
  77. memory_limit = 128M
  78. +max_memory_limit = -1
  79.  
  80. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  81. ; Error handling and logging ;
  82. diff -Naur a/php.ini-production b/php.ini-production
  83. --- a/php.ini-production 2025-09-19 10:08:21.746372837 +0200
  84. +++ b/php.ini-production 2025-09-19 10:08:48.160196778 +0200
  85. @@ -447,6 +447,7 @@
  86. ; Maximum amount of memory a script may consume
  87. ; https://php.net/memory-limit
  88. memory_limit = 128M
  89. +max_memory_limit = -1
  90.  
  91. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  92. ; Error handling and logging ;
  93. diff -Naur a/Zend/zend_ini.c b/Zend/zend_ini.c
  94. --- a/Zend/zend_ini.c 2025-08-26 17:05:30.000000000 +0200
  95. +++ b/Zend/zend_ini.c 2025-09-19 10:08:54.379155327 +0200
  96. @@ -247,10 +247,16 @@
  97. zend_unregister_ini_entries_ex(module_number, module_type);
  98. return FAILURE;
  99. }
  100. +
  101. + zend_string *prev_value = p->value;
  102. +
  103. if (((default_value = zend_get_configuration_directive(p->name)) != NULL) &&
  104. (!p->on_modify || p->on_modify(p, Z_STR_P(default_value), p->mh_arg1, p->mh_arg2, p->mh_arg3, ZEND_INI_STAGE_STARTUP) == SUCCESS)) {
  105.  
  106. - p->value = zend_new_interned_string(zend_string_copy(Z_STR_P(default_value)));
  107. + /* Skip assigning the value if the handler has already done so. */
  108. + if (p->value == prev_value) {
  109. + p->value = zend_new_interned_string(zend_string_copy(Z_STR_P(default_value)));
  110. + }
  111. } else {
  112. p->value = ini_entry->value ?
  113. zend_string_init_interned(ini_entry->value, ini_entry->value_length, 1) : NULL;
  114. @@ -388,14 +394,20 @@
  115. zend_hash_add_ptr(EG(modified_ini_directives), ini_entry->name, ini_entry);
  116. }
  117.  
  118. + zend_string *prev_value = ini_entry->value;
  119. duplicate = zend_string_copy(new_value);
  120.  
  121. if (!ini_entry->on_modify
  122. || ini_entry->on_modify(ini_entry, duplicate, ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, stage) == SUCCESS) {
  123. - if (modified && ini_entry->orig_value != ini_entry->value) { /* we already changed the value, free the changed value */
  124. - zend_string_release(ini_entry->value);
  125. + if (modified && ini_entry->orig_value != prev_value) { /* we already changed the value, free the changed value */
  126. + zend_string_release(prev_value);
  127. + }
  128. + /* Skip assigning the value if the handler has already done so. */
  129. + if (ini_entry->value == prev_value) {
  130. + ini_entry->value = duplicate;
  131. + } else {
  132. + zend_string_release(duplicate);
  133. }
  134. - ini_entry->value = duplicate;
  135. } else {
  136. zend_string_release(duplicate);
  137. return FAILURE;
  138.