Skip to content

Commit cd80ed6

Browse files
committed
Implement changes to GH-17951 according to ML discussion
1 parent 4e21924 commit cd80ed6

File tree

6 files changed

+37
-34
lines changed

6 files changed

+37
-34
lines changed

Zend/zend_ini.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,16 @@ ZEND_API zend_result zend_register_ini_entries_ex(const zend_ini_entry_def *ini_
248248
zend_unregister_ini_entries_ex(module_number, module_type);
249249
return FAILURE;
250250
}
251+
252+
zend_string *prev_value = p->value;
253+
251254
if (((default_value = zend_get_configuration_directive(p->name)) != NULL) &&
252255
(!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)) {
253256

254-
p->value = zend_new_interned_string(zend_string_copy(Z_STR_P(default_value)));
257+
/* Skip assigning the value if the handler has already done so. */
258+
if (p->value == prev_value) {
259+
p->value = zend_new_interned_string(zend_string_copy(Z_STR_P(default_value)));
260+
}
255261
} else {
256262
p->value = ini_entry->value ?
257263
zend_string_init_interned(ini_entry->value, ini_entry->value_length, 1) : NULL;
@@ -389,14 +395,20 @@ ZEND_API zend_result zend_alter_ini_entry_ex(zend_string *name, zend_string *new
389395
zend_hash_add_ptr(EG(modified_ini_directives), ini_entry->name, ini_entry);
390396
}
391397

398+
zend_string *prev_value = ini_entry->value;
392399
duplicate = zend_string_copy(new_value);
393400

394401
if (!ini_entry->on_modify
395402
|| ini_entry->on_modify(ini_entry, duplicate, ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, stage) == SUCCESS) {
396-
if (modified && ini_entry->orig_value != ini_entry->value) { /* we already changed the value, free the changed value */
397-
zend_string_release(ini_entry->value);
403+
if (modified && ini_entry->orig_value != prev_value) { /* we already changed the value, free the changed value */
404+
zend_string_release(prev_value);
405+
}
406+
/* Skip assigning the value if the handler has already done so. */
407+
if (ini_entry->value == prev_value) {
408+
ini_entry->value = duplicate;
409+
} else {
410+
zend_string_release(duplicate);
398411
}
399-
ini_entry->value = duplicate;
400412
} else {
401413
zend_string_release(duplicate);
402414
return FAILURE;

main/main.c

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -333,30 +333,19 @@ static PHP_INI_MH(OnChangeMemoryLimit)
333333
value = Z_L(1)<<30; /* effectively, no limit */
334334
}
335335

336-
/* If max_memory_limit is not set to unlimited, verify change */
337-
if (PG(max_memory_limit) != -1) {
338-
if (value == -1) {
339-
zend_error(
340-
E_WARNING,
341-
"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",
342-
PG(memory_limit),
343-
PG(max_memory_limit)
344-
);
345-
346-
return FAILURE;
336+
/* If memory_limit exceeds max_memory_limit, warn and set to max_memory_limit instead. */
337+
if (value > PG(max_memory_limit)) {
338+
if (value != -1) {
339+
zend_error(E_WARNING,
340+
"Failed to set memory_limit to %zd bytes. Setting to max_memory_limit instead (currently: " ZEND_LONG_FMT " bytes)",
341+
value, PG(max_memory_limit));
347342
}
348343

349-
if (value > PG(max_memory_limit)) {
350-
zend_error(
351-
E_WARNING,
352-
"Failed to set memory_limit to %zd bytes. memory_limit (currently: " ZEND_LONG_FMT " bytes) cannot exceed max_memory_limit (" ZEND_LONG_FMT " bytes)",
353-
value,
354-
PG(memory_limit),
355-
PG(max_memory_limit)
356-
);
344+
zend_ini_entry *max_mem_limit_ini = zend_hash_str_find_ptr(EG(ini_directives), ZEND_STRL("max_memory_limit"));
345+
entry->value = zend_string_copy(max_mem_limit_ini->value);
346+
PG(memory_limit) = PG(max_memory_limit);
357347

358-
return FAILURE;
359-
}
348+
return SUCCESS;
360349
}
361350

362351
if (zend_set_memory_limit(value) == FAILURE) {

tests/basic/gh17951_ini_parse_4.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ max_memory_limit=128M
99
<?php
1010
echo ini_get('max_memory_limit') . PHP_EOL;
1111
echo ini_get('memory_limit') . PHP_EOL;
12-
--EXPECTF--
13-
Warning: Failed to set memory_limit to unlimited. memory_limit (currently: %d bytes) cannot be set to unlimited if max_memory_limit (%d bytes) is not unlimited in %s
12+
?>
13+
--EXPECT--
1414
128M
1515
128M

tests/basic/gh17951_ini_parse_5.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ max_memory_limit=128M
99
<?php
1010
echo ini_get('max_memory_limit') . PHP_EOL;
1111
echo ini_get('memory_limit') . PHP_EOL;
12-
--EXPECTF--
13-
Warning: Failed to set memory_limit to %d bytes. memory_limit (currently: %d bytes) cannot exceed max_memory_limit (%d bytes) in %s
12+
?>
13+
--EXPECT--
14+
Warning: Failed to set memory_limit to 268435456 bytes. Setting to max_memory_limit instead (currently: 134217728 bytes) in Unknown on line 0
1415
128M
1516
128M

tests/basic/gh17951_runtime_change_3.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ max_memory_limit=512M
99
<?php
1010
ini_set('memory_limit', '1024M');
1111
echo ini_get('memory_limit');
12+
?>
1213
--EXPECTF--
13-
Warning: Failed to set memory_limit to %d bytes. memory_limit (currently: %d bytes) cannot exceed max_memory_limit (%d bytes) in %s
14-
128M
14+
Warning: Failed to set memory_limit to 1073741824 bytes. Setting to max_memory_limit instead (currently: 536870912 bytes) in %s on line %d
15+
512M

tests/basic/gh17951_runtime_change_4.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ max_memory_limit=512M
99
<?php
1010
ini_set('memory_limit', '-1');
1111
echo ini_get('memory_limit');
12-
--EXPECTF--
13-
Warning: Failed to set memory_limit to unlimited. memory_limit (currently: %d bytes) cannot be set to unlimited if max_memory_limit (%d bytes) is not unlimited in %s
14-
128M
12+
?>
13+
--EXPECT--
14+
512M

0 commit comments

Comments
 (0)