Skip to content

Commit 21c3cdf

Browse files
committed
NEWS entry, test and minor cleanup for FFI::isNull()
1 parent 825dac1 commit 21c3cdf

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ PHP NEWS
33
?? ??? ????, PHP 7.4.0RC2
44

55
- FFI:
6+
. Added missing FFI::isNull(). (Philip Hofstetter)
67
. Fixed bug #78488 (OOB in ZEND_FUNCTION(ffi_trampoline)). (Dmitry)
78

89
- Opcache:

ext/ffi/ffi.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4364,29 +4364,29 @@ ZEND_METHOD(FFI, string) /* {{{ */
43644364
ZEND_METHOD(FFI, isNull) /* {{{ */
43654365
{
43664366
zval *zv;
4367+
zend_ffi_cdata *cdata;
43674368
zend_ffi_type *type;
4368-
void *ptr = NULL;
43694369

43704370
ZEND_FFI_VALIDATE_API_RESTRICTION();
43714371
ZEND_PARSE_PARAMETERS_START(1, 1)
43724372
Z_PARAM_ZVAL(zv);
43734373
ZEND_PARSE_PARAMETERS_END();
43744374

43754375
ZVAL_DEREF(zv);
4376-
if (Z_TYPE_P(zv) == IS_OBJECT && Z_OBJCE_P(zv) == zend_ffi_cdata_ce) {
4377-
zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(zv);
4378-
type = ZEND_FFI_TYPE(cdata->type);
4379-
ptr = cdata->ptr;
4380-
} else {
4376+
if (Z_TYPE_P(zv) != IS_OBJECT || Z_OBJCE_P(zv) != zend_ffi_cdata_ce) {
43814377
zend_wrong_parameter_class_error(1, "FFI\\CData", zv);
43824378
return;
43834379
}
43844380

4381+
cdata = (zend_ffi_cdata*)Z_OBJ_P(zv);
4382+
type = ZEND_FFI_TYPE(cdata->type);
4383+
43854384
if (type->kind != ZEND_FFI_TYPE_POINTER){
43864385
zend_throw_error(zend_ffi_exception_ce, "FFI\\Cdata is not a pointer");
4386+
return;
43874387
}
43884388

4389-
RETURN_BOOL(*(void**)ptr == NULL);
4389+
RETURN_BOOL(*(void**)cdata->ptr == NULL);
43904390
}
43914391
/* }}} */
43924392

ext/ffi/tests/045.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
FFI 045: FFI::isNull()
3+
--SKIPIF--
4+
<?php require_once('skipif.inc'); ?>
5+
--INI--
6+
ffi.enable=1
7+
--FILE--
8+
<?php
9+
var_dump(FFI::isNull(FFI::new("int*")));
10+
$i = FFI::new("int");
11+
var_dump(FFI::isNull(FFI::addr($i)));
12+
try {
13+
var_dump(FFI::isNull(null));
14+
} catch (Throwable $e) {
15+
echo get_class($e) . ": " . $e->getMessage()."\n";
16+
}
17+
try {
18+
var_dump(FFI::isNull(FFI::new("int[0]")));
19+
} catch (Throwable $e) {
20+
echo get_class($e) . ": " . $e->getMessage()."\n";
21+
}
22+
?>
23+
--EXPECTF--
24+
bool(true)
25+
bool(false)
26+
27+
Warning: FFI::isNull() expects parameter 1 to be FFI\CData, null given in %s045.php on line %d
28+
NULL
29+
FFI\Exception: FFI\Cdata is not a pointer

0 commit comments

Comments
 (0)