Skip to content

Commit 3d783e8

Browse files
committed
Fix GH-9698: stream_wrapper_register crashes with FFI\CData provided as class
1 parent 62682cb commit 3d783e8

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

ext/ffi/ffi.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,10 @@ static zval *zend_ffi_cdata_write_field(zend_object *obj, zend_string *field_nam
12891289
if (cache_slot && *cache_slot == type) {
12901290
field = *(cache_slot + 1);
12911291
} else {
1292+
if (UNEXPECTED(type == NULL)) {
1293+
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign field '%s' to uninitialized FFI\\CData object", ZSTR_VAL(field_name));
1294+
return value;
1295+
}
12921296
if (type->kind == ZEND_FFI_TYPE_POINTER) {
12931297
type = ZEND_FFI_TYPE(type->pointer.type);
12941298
}

ext/ffi/tests/gh9698.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
GH-9698 (stream_wrapper_register crashes with FFI\CData provided as class)
3+
--EXTENSIONS--
4+
ffi
5+
--SKIPIF--
6+
<?php
7+
?>
8+
--FILE--
9+
<?php
10+
try {
11+
stream_wrapper_register('badffi', 'FFI\CData');
12+
file_get_contents('badffi://x');
13+
} catch (Throwable $exception) {
14+
echo $exception->getMessage();
15+
}
16+
?>
17+
18+
DONE
19+
--EXPECT--
20+
Attempt to assign field 'context' to uninitialized FFI\CData object
21+
DONE

main/streams/userspace.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,12 @@ static void user_stream_create_object(struct php_user_stream_wrapper *uwrap, php
304304
add_property_null(object, "context");
305305
}
306306

307+
if (EG(exception) != NULL) {
308+
zval_ptr_dtor(object);
309+
ZVAL_UNDEF(object);
310+
return;
311+
}
312+
307313
if (uwrap->ce->constructor) {
308314
zend_call_known_instance_method_with_0_params(
309315
uwrap->ce->constructor, Z_OBJ_P(object), NULL);

0 commit comments

Comments
 (0)