mirror of
https://github.com/php/php-src.git
synced 2025-08-15 21:48:51 +02:00
Throw in FFI::addr() when referencing temporary pointer
Closes GH-9601
This commit is contained in:
parent
c153ec877f
commit
d4ad9b7289
3 changed files with 52 additions and 0 deletions
|
@ -4215,6 +4215,11 @@ ZEND_METHOD(FFI, addr) /* {{{ */
|
|||
cdata = (zend_ffi_cdata*)Z_OBJ_P(zv);
|
||||
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->kind = ZEND_FFI_TYPE_POINTER;
|
||||
new_type->attr = 0;
|
||||
|
|
24
ext/ffi/tests/addr_to_owned.phpt
Normal file
24
ext/ffi/tests/addr_to_owned.phpt
Normal 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)
|
||||
}
|
||||
}
|
23
ext/ffi/tests/nested_addr.phpt
Normal file
23
ext/ffi/tests/nested_addr.phpt
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue