Skip to content

Commit 1cddc36

Browse files
Fix GH-15496: Fix accessing NULL pointer on output handler stack
1 parent b57578f commit 1cddc36

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
--TEST--
2+
GH-15496: Session handler ob_start crash
3+
--FILE--
4+
<?php
5+
class MySessionHandler implements SessionHandlerInterface
6+
{
7+
public function open ($save_path, $session_name): bool
8+
{
9+
return true;
10+
}
11+
12+
public function close(): bool
13+
{
14+
return true;
15+
}
16+
17+
public function read($id): string
18+
{
19+
return '';
20+
}
21+
22+
public function write($id, $sess_data): bool
23+
{
24+
ob_start(function () {});
25+
26+
return true;
27+
}
28+
29+
public function destroy($id): bool
30+
{
31+
return true;
32+
}
33+
34+
public function gc($maxlifetime): int
35+
{
36+
return 0;
37+
}
38+
}
39+
40+
session_set_save_handler(new MySessionHandler());
41+
session_start();
42+
43+
ob_start(function() {
44+
var_dump($b);
45+
});
46+
47+
while (1) {
48+
$a[] = 1;
49+
}
50+
?>
51+
--EXPECTF--
52+
Fatal error: {closure%A}(): Cannot use output buffering in output buffering display handlers in %s on line %d
53+
54+
Notice: ob_start(): Failed to create buffer in %s on line %d

main/output.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ PHPAPI int php_output_flush(void)
257257
if (OG(active) && (OG(active)->flags & PHP_OUTPUT_HANDLER_FLUSHABLE)) {
258258
php_output_context_init(&context, PHP_OUTPUT_HANDLER_FLUSH);
259259
php_output_handler_op(OG(active), &context);
260-
if (context.out.data && context.out.used) {
260+
if (context.out.data && context.out.used && OG(flags) & PHP_OUTPUT_ACTIVATED) {
261261
zend_stack_del_top(&OG(handlers));
262262
php_output_write(context.out.data, context.out.used);
263263
zend_stack_push(&OG(handlers), &OG(active));
@@ -538,6 +538,9 @@ PHPAPI int php_output_handler_start(php_output_handler *handler)
538538
if (php_output_lock_error(PHP_OUTPUT_HANDLER_START) || !handler) {
539539
return FAILURE;
540540
}
541+
if (!(OG(flags) & PHP_OUTPUT_ACTIVATED)) {
542+
return FAILURE;
543+
}
541544
if (NULL != (conflict = zend_hash_find_ptr(&php_output_handler_conflicts, handler->name))) {
542545
if (SUCCESS != conflict(ZSTR_VAL(handler->name), ZSTR_LEN(handler->name))) {
543546
return FAILURE;

0 commit comments

Comments
 (0)