Skip to content

Commit 1e2d3d5

Browse files
committed
Fix #78762: Failing FFI::cast() may leak memory
We have to release objects when we're done with them.
1 parent c76dbef commit 1e2d3d5

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ PHP NEWS
1818
- FFI:
1919
. Fixed bug #78716 (Function name mangling is wrong for some parameter
2020
types). (cmb)
21+
. Fixed bug #78762 (Failing FFI::cast() may leak memory). (cmb)
2122
. Implement FR #78270 (Support __vectorcall convention with FFI). (cmb)
2223

2324
- FPM:

ext/ffi/ffi.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3882,6 +3882,7 @@ ZEND_METHOD(FFI, cast) /* {{{ */
38823882
cdata->ptr = &cdata->ptr_holder;
38833883
cdata->ptr_holder = old_cdata->ptr;
38843884
} else if (type->size > old_type->size) {
3885+
zend_object_release(&cdata->std);
38853886
zend_throw_error(zend_ffi_exception_ce, "attempt to cast to larger type");
38863887
return;
38873888
} else if (ptr != &old_cdata->ptr_holder) {

ext/ffi/tests/bug78762.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Bug #78762 (Failing FFI::cast() may leak memory)
3+
--SKIPIF--
4+
<?php require_once('skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
try {
8+
FFI::cast('char[10]', FFI::new('char[1]'));
9+
} catch (FFI\Exception $ex) {
10+
echo $ex->getMessage(), PHP_EOL;
11+
}
12+
?>
13+
--EXPECT--
14+
attempt to cast to larger type

0 commit comments

Comments
 (0)