Skip to content

Fix error handling restoration, fiber safety, and stream exceptions in parse_file() - #266

Open
iliaal wants to merge 1 commit into
nikic:masterfrom
iliaal:fix/parse-file-error-handling
Open

iliaal wants to merge 1 commit into
nikic:masterfrom
iliaal:fix/parse-file-error-handling

Conversation

@iliaal

@iliaal iliaal commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Before

PHP_FUNCTION(parse_file) held zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling) across php_stream_open_wrapper_ex(), php_stream_copy_to_mem(), and php_stream_close(). Because userland stream wrappers can run inside those calls:

  • On PHP 8.1+, suspending a Fiber inside a stream wrapper switched fibers while EH_THROW was active (fibers do not save/restore EG(error_handling)), leaking EH_THROW to other fibers and allowing parse_file() to return without throwing.
  • A fatal bailout (zend_bailout()) during stream I/O skipped zend_restore_error_handling(), leaving EH_THROW active during shutdown functions.
  • An exception thrown during stream read or close was ignored, causing parse_file() to enter get_ast() with EG(exception) already set (tripping zendlex()'s debug assertion and chaining a ParseError on invalid input).
  • If stream opening failed without setting EG(exception) (such as under PHP 8.6 StreamErrorMode::Silent via stream_context_get_default()), parse_file() returned null despite its non-nullable \ast\Node return type.

After

  • Wrapped stream I/O in zend_fiber_switch_block() / zend_fiber_switch_unblock() on PHP 8.1+ and zend_try / zend_catch, releasing any copied code buffer and restoring error handling and fiber switching before re-invoking zend_bailout().
  • Scoped php_stream *stream inside the zend_try block and tracked open success via volatile zend_bool opened so the closed stream pointer is never inspected after php_stream_close().
  • Added a fallback RuntimeException when opening fails with EG(exception) == NULL, and aborted before get_ast() (freeing code) whenever EG(exception) is set after stream read or close.

Scope rationale

  • Confined to PHP_FUNCTION(parse_file) stream I/O lifecycle; parse_code() does not perform stream I/O or replace error handling.
  • Does not modify package.xml, matching repository convention for PRs.

Test plan

  • Added tests/parse_file_fiber.phpt, tests/parse_file_stream_exception.phpt, tests/parse_file_bailout_error_handling.phpt, and tests/parse_file_silent_open.phpt (verified failures on master, including leaked EH_THROW and zendlex() abort)
  • Built with -Wall -Wextra -Wno-unused-parameter -Werror (zero -Wclobbered warnings) and passed full test suite on PHP 7.4 debug, 8.4 ASan NTS/ZTS, 8.5 debug/release, and 8.6 debug
  • Ran Valgrind (run-tests.php -m) on the new PHP 8.5-applicable tests: 0 errors, 0 leaks
  • Verified pecl package succeeds

…n parse_file()

ast\parse_file() held zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, ...) across stream open, read, and close without blocking fiber switches or catching bailouts, and proceeded into get_ast() even when stream read or close threw an exception.

Wrap stream I/O in zend_fiber_switch_block()/unblock() (PHP >= 8.1) and zend_try/zend_catch so error handling and fiber switching are always restored and any copied buffer is freed on bailout. Check EG(exception) before calling get_ast(), avoid inspecting the stream pointer after closing, and throw RuntimeException if opening fails without setting an exception.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant