From 4318982787ab999d431add56c8bc9296956b83d5 Mon Sep 17 00:00:00 2001 From: Alexander Lisachenko Date: Mon, 27 Jan 2020 20:09:03 +0200 Subject: [PATCH] Fix #79177 Check for undefined result in zend_ffi_callback_trampoline. This situation can happen when userland exception is thrown within callback body. --- ext/ffi/ffi.c | 2 +- ext/ffi/tests/bug79177.phpt | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 ext/ffi/tests/bug79177.phpt diff --git a/ext/ffi/ffi.c b/ext/ffi/ffi.c index f43ba2adc8d27..cafe837351342 100644 --- a/ext/ffi/ffi.c +++ b/ext/ffi/ffi.c @@ -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); } } diff --git a/ext/ffi/tests/bug79177.phpt b/ext/ffi/tests/bug79177.phpt new file mode 100644 index 0000000000000..0f541d763a794 --- /dev/null +++ b/ext/ffi/tests/bug79177.phpt @@ -0,0 +1,32 @@ +--TEST-- +Bug #79177 (FFI doesn't handle well PHP exceptions within callback body) +--SKIPIF-- + +--INI-- +ffi.enable=1 +--FILE-- +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