Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Zend/tests/zpp/gh24491.phpt

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this to the zpp subfolder.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it to Zend/tests/zpp, thanks.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
GH-24491: Callable error string leaks when an exception is thrown during zpp
--FILE--
<?php
set_error_handler(function (int $errno, string $errstr) {
throw new Exception($errstr);
});

class A {
public static function foo() {}

public static function test() {
register_shutdown_function(['self', 'foo']);
}
}

try {
A::test();
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
Use of "self" in callables is deprecated
3 changes: 3 additions & 0 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,9 @@ static zend_result zend_parse_arg(uint32_t arg_num, zval *arg, va_list *va, cons
zend_expected_type expected_type = zend_parse_arg_impl(arg, va, spec, &error, arg_num);
if (expected_type != Z_EXPECTED_LAST) {
if (EG(exception)) {
if (error && (expected_type == Z_EXPECTED_FUNC || expected_type == Z_EXPECTED_FUNC_OR_NULL)) {
efree(error);
}
return FAILURE;
}

Expand Down
31 changes: 31 additions & 0 deletions ext/standard/tests/streams/gh24491.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
GH-24491 (Stream error_handler given as 'Class::method' turns the real exception into a TypeError)
--FILE--
<?php
class H {
public static function handle(array $errors): void {
echo "handler called\n";
}
}

function run(array $options): void {
try {
fopen('/nonexistent/dir/file', 'r', false, stream_context_create(['stream' => $options]));
} catch (Throwable $e) {
echo $e::class, ": ", $e->getMessage(), "\n";
var_dump($e->getPrevious());
}
}

run(['error_mode' => StreamErrorMode::Exception, 'error_handler' => 'H::handle']);

set_error_handler(function (int $errno, string $errstr) {
throw new ErrorException($errstr);
});
run(['error_handler' => ['H', 'handle']]);
?>
--EXPECT--
StreamException: Failed to open stream: No such file or directory
NULL
ErrorException: fopen(): Failed to open stream: No such file or directory
NULL
4 changes: 3 additions & 1 deletion main/streams/stream_errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,9 @@ static void php_stream_call_error_handler(const zval *handler, zval *errors_arra
char *is_callable_error = NULL;

if (!zend_is_callable(handler, &fcc, &is_callable_error)) {
zend_type_error("stream error handler must be a valid callback, %s", is_callable_error);
if (!EG(exception)) {
zend_type_error("stream error handler must be a valid callback, %s", is_callable_error);
}
efree(is_callable_error);
return;
}
Expand Down
Loading