Throw in FFI::addr() when referencing temporary pointer

Closes GH-9601
This commit is contained in:
Ilija Tovilo 2022-09-23 15:42:57 +02:00
parent c153ec877f
commit d4ad9b7289
No known key found for this signature in database
GPG key ID: A4F5D403F118200A
3 changed files with 52 additions and 0 deletions

View file

@ -4215,6 +4215,11 @@ ZEND_METHOD(FFI, addr) /* {{{ */
cdata = (zend_ffi_cdata*)Z_OBJ_P(zv); cdata = (zend_ffi_cdata*)Z_OBJ_P(zv);
type = ZEND_FFI_TYPE(cdata->type); type = ZEND_FFI_TYPE(cdata->type);
if (GC_REFCOUNT(&cdata->std) == 1 && Z_REFCOUNT_P(arg) == 1 && type->kind == ZEND_FFI_TYPE_POINTER) {
zend_throw_error(zend_ffi_exception_ce, "FFI::addr() cannot create a reference to a temporary pointer");
RETURN_THROWS();
}
new_type = emalloc(sizeof(zend_ffi_type)); new_type = emalloc(sizeof(zend_ffi_type));
new_type->kind = ZEND_FFI_TYPE_POINTER; new_type->kind = ZEND_FFI_TYPE_POINTER;
new_type->attr = 0; new_type->attr = 0;

View file

@ -0,0 +1,24 @@
--TEST--
FFI Referencing temporary owned data transfers ownership
--EXTENSIONS--
ffi
--INI--
ffi.enable=1
--FILE--
<?php
$ffi = \FFI::cdef(<<<'CPP'
typedef struct {
int8_t bar;
} Foo;
CPP);
$structPtr = \FFI::addr($ffi->new('Foo'));
var_dump($structPtr);
?>
--EXPECT--
object(FFI\CData:struct <anonymous>*)#3 (1) {
[0]=>
object(FFI\CData:struct <anonymous>)#2 (1) {
["bar"]=>
int(0)
}
}

View file

@ -0,0 +1,23 @@
--TEST--
FFI Cannot nest FFI::addr() calls
--EXTENSIONS--
ffi
--INI--
ffi.enable=1
--FILE--
<?php
$ffi = \FFI::cdef(<<<'CPP'
typedef struct {
int8_t bar;
} Foo;
CPP);
$struct = $ffi->new('Foo');
$structPtrPtr = \FFI::addr(\FFI::addr($struct));
?>
--EXPECTF--
Fatal error: Uncaught FFI\Exception: FFI::addr() cannot create a reference to a temporary pointer in %s:%d
Stack trace:
#0 %s(%d): FFI::addr(Object(FFI\CData:struct <anonymous>*))
#1 {main}
thrown in %s on line %d