Skip to content

Commit 7d4de1a

Browse files
committed
Fixed possible crash
1 parent 62fd45f commit 7d4de1a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

ext/ffi/ffi.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1138,10 +1138,16 @@ static void zend_ffi_cdata_write_dim(zval *object, zval *offset, zval *value) /*
11381138
{
11391139
zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(object);
11401140
zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
1141-
zend_long dim = zval_get_long(offset);
1141+
zend_long dim;
11421142
void *ptr;
11431143
zend_ffi_flags is_const;
11441144

1145+
if (offset == NULL) {
1146+
zend_throw_error(zend_ffi_exception_ce, "Cannot add next element to object of type FFI\\CData");
1147+
return;
1148+
}
1149+
1150+
dim = zval_get_long(offset);
11451151
if (EXPECTED(type->kind == ZEND_FFI_TYPE_ARRAY)) {
11461152
if (UNEXPECTED((zend_ulong)(dim) >= (zend_ulong)type->array.length)
11471153
&& (UNEXPECTED(dim < 0) || UNEXPECTED(type->array.length != 0))) {

ext/ffi/tests/042.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
FFI 042: Next array element
3+
--SKIPIF--
4+
<?php require_once('skipif.inc'); ?>
5+
--INI--
6+
ffi.enable=1
7+
--FILE--
8+
<?php
9+
$a = FFI::new("uint8_t[8]");
10+
$a[] = 0;
11+
?>
12+
--EXPECTF--
13+
Fatal error: Uncaught FFI\Exception: Cannot add next element to object of type FFI\CData in %sext/ffi/tests/042.php:3
14+
Stack trace:
15+
#0 {main}
16+
thrown in %sext/ffi/tests/042.php on line 3

0 commit comments

Comments
 (0)