php / php-src

The PHP Interpreter
https://www.php.net
Other
38.1k stars 7.74k forks source link

Segmentation fault (access null pointer) in Zend/zend_stack.c #15496

Open YuanchengJiang opened 2 months ago

YuanchengJiang commented 2 months ago

Description

The following code:

<?php
class MySessionHandler implements SessionHandlerInterface {
    public function open ($save_path, $session_name): bool {
        return true;
    }
    public function close(): bool {}
    public function read($id): string {
        return '';
    }
    public function write($id, $sess_data): bool {
        ob_start(function () {});
    }
    public function destroy($id): bool {}
    public function gc($maxlifetime): int {}
}

session_set_save_handler(new MySessionHandler());
session_start();

ob_start(function() {
    var_dump($b);
});

while (1) {
    $a[] = 1;
}

Resulted in this output:

/php-src/Zend/zend_stack.c:40:9: runtime error: applying zero offset to null pointer
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /php-src/Zend/zend_stack.c:40:9

PHP Version

PHP 8.4.0-dev

Operating System

ubuntu 22.04

cmb69 commented 2 months ago

From a quick look, that seems to be another output buffering issue.

iluuu1994 commented 1 month ago

I had a look, and what I believe is happening:

With all of that said, I don't know what the best approach is to fix this. Maybe we just don't trigger the handler when cleaning the output? I really don't know much about output buffering.