Skip to content

Fix #79177 Check for undefined result in zend_ffi_callback_trampoline. #5120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
2 changes: 1 addition & 1 deletion ext/ffi/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ static void zend_ffi_callback_trampoline(ffi_cif* cif, void* ret, void** args, v
free_alloca(fci.params, use_heap);

ret_type = ZEND_FFI_TYPE(callback_data->type->func.ret_type);
if (ret_type->kind != ZEND_FFI_TYPE_VOID) {
if (ret_type->kind != ZEND_FFI_TYPE_VOID && Z_TYPE(retval) != IS_UNDEF) {
zend_ffi_zval_to_cdata(ret, ret_type, &retval);
}
}
Expand Down
32 changes: 32 additions & 0 deletions ext/ffi/tests/bug79177.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
Bug #79177 (FFI doesn't handle well PHP exceptions within callback body)
--SKIPIF--
<?php require_once('skipif.inc');?>
--INI--
ffi.enable=1
--FILE--
<?php

use FFI\CData;

$php = FFI::cdef("
typedef unsigned int fake_struct;
typedef fake_struct* (*zend_write_func_t)(const char *str, size_t str_length);
extern zend_write_func_t zend_write;
");

$originalHandler = clone $php->zend_write;
$php->zend_write = function($str, $len): CData {
throw new \RuntimeException('Not allowed');
};
try {
echo "After", PHP_EOL;
} catch (\Throwable $exception) {
// Do not output anything here, as handler is overridden
} finally {
$php->zend_write = $originalHandler;
}
echo get_class($exception), ': ', $exception->getMessage(), PHP_EOL;
?>
--EXPECT--
RuntimeException: Not allowed