Skip to content

Commit cc489ea

Browse files
committed
Fix GH-20906: Assertion failure when messing up output buffers
1 parent a6e0d8e commit cc489ea

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

ext/standard/basic_functions.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,7 +1768,10 @@ PHP_FUNCTION(highlight_file)
17681768
}
17691769

17701770
if (i) {
1771-
php_output_start_default();
1771+
if (php_output_start_default() != SUCCESS) {
1772+
zend_throw_error(NULL, "Unable to start output handler");
1773+
RETURN_THROWS();
1774+
}
17721775
}
17731776

17741777
php_get_highlight_struct(&syntax_highlighter_ini);
@@ -1803,7 +1806,10 @@ PHP_FUNCTION(php_strip_whitespace)
18031806
Z_PARAM_PATH_STR(filename)
18041807
ZEND_PARSE_PARAMETERS_END();
18051808

1806-
php_output_start_default();
1809+
if (php_output_start_default() != SUCCESS) {
1810+
zend_throw_error(NULL, "Unable to start output handler");
1811+
RETURN_THROWS();
1812+
}
18071813

18081814
zend_stream_init_filename_ex(&file_handle, filename);
18091815
zend_save_lexical_state(&original_lex_state);
@@ -1840,7 +1846,10 @@ PHP_FUNCTION(highlight_string)
18401846
ZEND_PARSE_PARAMETERS_END();
18411847

18421848
if (i) {
1843-
php_output_start_default();
1849+
if (php_output_start_default() != SUCCESS) {
1850+
zend_throw_error(NULL, "Unable to start output handler");
1851+
RETURN_THROWS();
1852+
}
18441853
}
18451854

18461855
EG(error_reporting) = E_ERROR;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
GH-20906 (Assertion failure when messing up output buffers) - php_strip_whitespace
3+
--CREDITS--
4+
vi3tL0u1s
5+
--FILE--
6+
<?php
7+
class A {
8+
function __destruct() {
9+
php_strip_whitespace(__FILE__);
10+
echo "x";
11+
$c = new A;
12+
ob_start(function () use ($c) { return '/'; }, 1);
13+
ob_start(function () use (&$c) { $c = new A; return '/'; }, 1);
14+
}
15+
}
16+
17+
try {
18+
new A;
19+
} catch (Throwable $e) {
20+
echo $e::class, ": ", $e->getMessage(), "\n";
21+
}
22+
?>
23+
--EXPECTF--
24+
%a
25+
Fatal error: php_strip_whitespace(): Cannot use output buffering in output buffering display handlers in %s on line %d
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
GH-20906 (Assertion failure when messing up output buffers) - highlight_file
3+
--CREDITS--
4+
vi3tL0u1s
5+
--FILE--
6+
<?php
7+
class A {
8+
function __destruct() {
9+
highlight_file(__FILE__, true);
10+
echo "x";
11+
$c = new A;
12+
ob_start(function () use ($c) { return '/'; }, 1);
13+
ob_start(function () use (&$c) { $c = new A; return '/'; }, 1);
14+
}
15+
}
16+
17+
new A;
18+
?>
19+
--EXPECTF--
20+
%a
21+
Fatal error: highlight_file(): Cannot use output buffering in output buffering display handlers in %s on line %d
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
GH-20906 (Assertion failure when messing up output buffers) - highlight_string
3+
--CREDITS--
4+
vi3tL0u1s
5+
--FILE--
6+
<?php
7+
class A {
8+
function __destruct() {
9+
highlight_string(__FILE__, true);
10+
echo "x";
11+
$c = new A;
12+
ob_start(function () use ($c) { return '/'; }, 1);
13+
ob_start(function () use (&$c) { $c = new A; return '/'; }, 1);
14+
}
15+
}
16+
17+
new A;
18+
?>
19+
--EXPECTF--
20+
%a
21+
Fatal error: highlight_string(): Cannot use output buffering in output buffering display handlers in %s on line %d

0 commit comments

Comments
 (0)