Skip to content

Commit 327e6c9

Browse files
committed
Error on unhandled exceptions in FFI callbacks
This has been discussed and agreed upon in a previous PR[1]. [1] <php#5120>
1 parent 05aef89 commit 327e6c9

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

ext/ffi/ffi.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,8 @@ static void zend_ffi_callback_trampoline(ffi_cif* cif, void* ret, void** args, v
887887
free_alloca(fci.params, use_heap);
888888

889889
if (EG(exception)) {
890-
return;
890+
zend_throw_error(zend_ffi_exception_ce, "Uncaught %s in PHP FFI callback", ZSTR_VAL(EG(exception)->ce->name));
891+
zend_exception_error(EG(exception), E_ERROR);
891892
}
892893

893894
ret_type = ZEND_FFI_TYPE(callback_data->type->func.ret_type);

ext/ffi/tests/bug79177.phpt

+13-4
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ typedef char (*zend_write_func_t)(const char *str, size_t str_length);
1818
extern zend_write_func_t zend_write;
1919
", ffi_get_php_dll_name());
2020

21-
echo "Before", PHP_EOL;
21+
echo "Before\n";
2222

2323
$originalHandler = clone $php->zend_write;
2424
$php->zend_write = function($str, $len): string {
2525
throw new \RuntimeException('Not allowed');
2626
};
2727
try {
28-
echo "After", PHP_EOL;
28+
echo "After\n";
2929
} catch (\Throwable $exception) {
3030
// Do not output anything here, as handler is overridden
3131
} finally {
@@ -35,6 +35,15 @@ if (isset($exception)) {
3535
echo $exception->getMessage(), PHP_EOL;
3636
}
3737
?>
38-
--EXPECT--
38+
--EXPECTF--
3939
Before
40-
Not allowed
40+
41+
Fatal error: Uncaught RuntimeException: Not allowed in %s:%d
42+
Stack trace:
43+
#0 %s(15): {closure}('After\n', 6)
44+
#1 {main}
45+
46+
Next FFI\Exception: Uncaught RuntimeException in PHP FFI callback in %s:%d
47+
Stack trace:
48+
#0 {main}
49+
thrown in %s on line %d

0 commit comments

Comments
 (0)