Skip to content

Commit 54ecf57

Browse files
committed
Disable instantiation of zero size FFI\CData objects
1 parent b01b1f6 commit 54ecf57

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

ext/ffi/ffi.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3685,6 +3685,12 @@ ZEND_METHOD(FFI, new) /* {{{ */
36853685
}
36863686
}
36873687

3688+
if (type->size == 0) {
3689+
zend_throw_error(zend_ffi_exception_ce, "Cannot instantiate FFI\\CData of zero size");
3690+
zend_ffi_type_dtor(type_ptr);
3691+
return;
3692+
}
3693+
36883694
ptr = pemalloc(type->size, flags & ZEND_FFI_FLAG_PERSISTENT);
36893695
memset(ptr, 0, type->size);
36903696

ext/ffi/tests/023.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ FFI 023: GCC struct extensions
66
ffi.enable=1
77
--FILE--
88
<?php
9-
var_dump(FFI::sizeof(FFI::new("struct {}")));
9+
try {
10+
var_dump(FFI::sizeof(FFI::new("struct {}")));
11+
} catch (Throwable $e) {
12+
echo get_class($e) . ": " . $e->getMessage() . "\n";
13+
}
1014
var_dump(FFI::sizeof(FFI::new("struct {int a}")));
1115
var_dump(FFI::sizeof(FFI::new("struct {int a; int b}")));
1216
?>
1317
ok
1418
--EXPECT--
15-
int(0)
19+
FFI\Exception: Cannot instantiate FFI\CData of zero size
1620
int(4)
1721
int(8)
1822
ok

ext/ffi/tests/027.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ FFI\ParserException: '[*]' not allowed in other than function prototype scope at
8181
FFI\ParserException: '[*]' not allowed in other than function prototype scope at line 1
8282
FFI\ParserException: '[*]' not allowed in other than function prototype scope at line 1
8383
ok
84-
int(0)
84+
FFI\Exception: Cannot instantiate FFI\CData of zero size
8585
FFI\ParserException: '[]' not allowed at line 1
8686
FFI\ParserException: '[]' not allowed at line 1
8787
ok

ext/ffi/tests/045.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ bool(false)
2626

2727
Warning: FFI::isNull() expects parameter 1 to be FFI\CData, null given in %s045.php on line %d
2828
NULL
29-
FFI\Exception: FFI\Cdata is not a pointer
29+
FFI\Exception: Cannot instantiate FFI\CData of zero size

0 commit comments

Comments
 (0)