Skip to content

Commit e9b0051

Browse files
committed
Fix output buffer discard on memory limit
Move this code directly into the error handler, and check the heap->overflow flag. Discarding output here allows us to print the normal memory limit message to standard output. Otherwise nothing would be printed unless a different log medium was used, which makes for a suboptimal debugging experience.
1 parent 3a4ea6c commit e9b0051

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

Zend/zend_alloc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,6 +2669,15 @@ ZEND_API zend_result zend_set_memory_limit(size_t memory_limit)
26692669
return SUCCESS;
26702670
}
26712671

2672+
ZEND_API bool zend_alloc_in_memory_limit_error_reporting(void)
2673+
{
2674+
#if ZEND_MM_LIMIT
2675+
return AG(mm_heap)->overflow;
2676+
#else
2677+
return false;
2678+
#endif
2679+
}
2680+
26722681
ZEND_API size_t zend_memory_usage(bool real_usage)
26732682
{
26742683
#if ZEND_MM_STAT

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 zend_result zend_set_memory_limit(size_t memory_limit);
223+
ZEND_API bool zend_alloc_in_memory_limit_error_reporting(void);
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: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,10 @@ static ZEND_COLD void php_error_cb(int orig_type, zend_string *error_filename, c
12351235
PG(last_error_lineno) = error_lineno;
12361236
}
12371237

1238+
if (zend_alloc_in_memory_limit_error_reporting()) {
1239+
php_output_discard_all();
1240+
}
1241+
12381242
/* display/log the error if necessary */
12391243
if (display && ((EG(error_reporting) & type) || (type & E_CORE))
12401244
&& (PG(log_errors) || PG(display_errors) || (!module_initialized))) {
@@ -1783,19 +1787,7 @@ void php_request_shutdown(void *dummy)
17831787

17841788
/* 3. Flush all output buffers */
17851789
zend_try {
1786-
bool send_buffer = SG(request_info).headers_only ? 0 : 1;
1787-
1788-
if (CG(unclean_shutdown) && PG(last_error_type) == E_ERROR &&
1789-
(size_t)PG(memory_limit) < zend_memory_usage(1)
1790-
) {
1791-
send_buffer = 0;
1792-
}
1793-
1794-
if (!send_buffer) {
1795-
php_output_discard_all();
1796-
} else {
1797-
php_output_end_all();
1798-
}
1790+
php_output_end_all();
17991791
} zend_end_try();
18001792

18011793
/* 4. Reset max_execution_time (no longer executing php code after response sent) */

tests/lang/bug45392.phpt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
--TEST--
22
Bug #45392 (ob_start()/ob_end_clean() and memory_limit)
3-
--INI--
4-
display_errors=stderr
5-
--XFAIL--
6-
The issue has not yet been resolved.
73
--SKIPIF--
84
<?php
95
if (getenv("USE_ZEND_ALLOC") === "0") {
@@ -22,4 +18,5 @@ ob_end_clean();
2218
?>
2319
--EXPECTF--
2420
2
21+
2522
Fatal error: Allowed memory size of %d bytes exhausted%s

0 commit comments

Comments
 (0)