Skip to content

Commit 97bb48e

Browse files
committed
ext/ffi: Fix resource leak in FFI::cdef() on symbol resolution failure.
and remove duplicate conditions in read/write field handlers. close GH-21446
1 parent c920daa commit 97bb48e

File tree

5 files changed

+60
-25
lines changed

5 files changed

+60
-25
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ PHP NEWS
1313
. Fixed bug GH-21486 (Dom\HTMLDocument parser mangles xml:space and
1414
xml:lang attributes). (ndossche)
1515

16+
- FFI:
17+
. Fixed resource leak in FFI::cdef() onsymbol resolution failure.
18+
(David Carlier)
19+
1620
- GD:
1721
. Fixed bug GH-21431 (phpinfo() to display libJPEG 10.0 support).
1822
(David Carlier)

ext/ffi/ffi.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,10 +1257,8 @@ static zval *zend_ffi_cdata_read_field(zend_object *obj, zend_string *field_name
12571257
type = ZEND_FFI_TYPE(type->pointer.type);
12581258
}
12591259
if (UNEXPECTED(type->kind != ZEND_FFI_TYPE_STRUCT)) {
1260-
if (UNEXPECTED(type->kind != ZEND_FFI_TYPE_STRUCT)) {
1261-
zend_throw_error(zend_ffi_exception_ce, "Attempt to read field '%s' of non C struct/union", ZSTR_VAL(field_name));
1262-
return &EG(uninitialized_zval);
1263-
}
1260+
zend_throw_error(zend_ffi_exception_ce, "Attempt to read field '%s' of non C struct/union", ZSTR_VAL(field_name));
1261+
return &EG(uninitialized_zval);
12641262
}
12651263

12661264
field = zend_hash_find_ptr(&type->record.fields, field_name);
@@ -1334,10 +1332,8 @@ static zval *zend_ffi_cdata_write_field(zend_object *obj, zend_string *field_nam
13341332
type = ZEND_FFI_TYPE(type->pointer.type);
13351333
}
13361334
if (UNEXPECTED(type->kind != ZEND_FFI_TYPE_STRUCT)) {
1337-
if (UNEXPECTED(type->kind != ZEND_FFI_TYPE_STRUCT)) {
1338-
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign field '%s' of non C struct/union", ZSTR_VAL(field_name));
1339-
return value;
1340-
}
1335+
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign field '%s' of non C struct/union", ZSTR_VAL(field_name));
1336+
return value;
13411337
}
13421338

13431339
field = zend_hash_find_ptr(&type->record.fields, field_name);
@@ -3069,17 +3065,7 @@ ZEND_METHOD(FFI, cdef) /* {{{ */
30693065
FFI_G(default_type_attr) = ZEND_FFI_ATTR_STORED;
30703066

30713067
if (zend_ffi_parse_decl(ZSTR_VAL(code), ZSTR_LEN(code)) == FAILURE) {
3072-
if (FFI_G(symbols)) {
3073-
zend_hash_destroy(FFI_G(symbols));
3074-
efree(FFI_G(symbols));
3075-
FFI_G(symbols) = NULL;
3076-
}
3077-
if (FFI_G(tags)) {
3078-
zend_hash_destroy(FFI_G(tags));
3079-
efree(FFI_G(tags));
3080-
FFI_G(tags) = NULL;
3081-
}
3082-
RETURN_THROWS();
3068+
goto cleanup;
30833069
}
30843070

30853071
if (FFI_G(symbols)) {
@@ -3091,7 +3077,7 @@ ZEND_METHOD(FFI, cdef) /* {{{ */
30913077
addr = DL_FETCH_SYMBOL(handle, ZSTR_VAL(name));
30923078
if (!addr) {
30933079
zend_throw_error(zend_ffi_exception_ce, "Failed resolving C variable '%s'", ZSTR_VAL(name));
3094-
RETURN_THROWS();
3080+
goto cleanup;
30953081
}
30963082
sym->addr = addr;
30973083
} else if (sym->kind == ZEND_FFI_SYM_FUNC) {
@@ -3101,7 +3087,7 @@ ZEND_METHOD(FFI, cdef) /* {{{ */
31013087
zend_string_release(mangled_name);
31023088
if (!addr) {
31033089
zend_throw_error(zend_ffi_exception_ce, "Failed resolving C function '%s'", ZSTR_VAL(name));
3104-
RETURN_THROWS();
3090+
goto cleanup;
31053091
}
31063092
sym->addr = addr;
31073093
}
@@ -3118,6 +3104,22 @@ ZEND_METHOD(FFI, cdef) /* {{{ */
31183104
FFI_G(tags) = NULL;
31193105

31203106
RETURN_OBJ(&ffi->std);
3107+
3108+
cleanup:
3109+
if (lib && handle) {
3110+
DL_UNLOAD(handle);
3111+
}
3112+
if (FFI_G(symbols)) {
3113+
zend_hash_destroy(FFI_G(symbols));
3114+
efree(FFI_G(symbols));
3115+
FFI_G(symbols) = NULL;
3116+
}
3117+
if (FFI_G(tags)) {
3118+
zend_hash_destroy(FFI_G(tags));
3119+
efree(FFI_G(tags));
3120+
FFI_G(tags) = NULL;
3121+
}
3122+
RETURN_THROWS();
31213123
}
31223124
/* }}} */
31233125

ext/ffi/tests/gh14286_2.phpt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
GH-14286 (ffi enum type (when enum has no name) make memory leak)
33
--EXTENSIONS--
44
ffi
5-
--SKIPIF--
6-
<?php
7-
if (PHP_DEBUG || getenv('SKIP_ASAN')) die("xfail: FFI cleanup after parser error is not implemented");
8-
?>
95
--INI--
106
ffi.enable=1
117
--FILE--
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
GH-18629 (FFI::cdef() leaks on function resolution failure)
3+
--EXTENSIONS--
4+
ffi
5+
--INI--
6+
ffi.enable=1
7+
--FILE--
8+
<?php
9+
try {
10+
$ffi = FFI::cdef("void nonexistent_ffi_test_func(void);");
11+
} catch (\FFI\Exception $e) {
12+
echo $e->getMessage() . "\n";
13+
}
14+
?>
15+
--EXPECT--
16+
Failed resolving C function 'nonexistent_ffi_test_func'
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
GH-18629 (Read field of non C struct/union)
3+
--EXTENSIONS--
4+
ffi
5+
--INI--
6+
ffi.enable=1
7+
--FILE--
8+
<?php
9+
$x = FFI::cdef()->new("int*");
10+
try {
11+
$y = $x->foo;
12+
} catch (\FFI\Exception $e) {
13+
echo $e->getMessage() . "\n";
14+
}
15+
?>
16+
--EXPECT--
17+
Attempt to read field 'foo' of non C struct/union

0 commit comments

Comments
 (0)