Skip to content

Commit

Permalink
Fix GH-9698: stream_wrapper_register crashes with FFI\CData provided …
Browse files Browse the repository at this point in the history
…as class

Closes GH-12926
  • Loading branch information
bukka committed Dec 15, 2023
1 parent 2ee4d35 commit 40ccc8e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NEWS
Expand Up @@ -6,6 +6,10 @@ PHP NEWS
. Fix incorrect timeout in built-in web server when using router script and
max_input_time. (ilutov)

- FFI:
. Fixed bug GH-9698 (stream_wrapper_register crashes with FFI\CData).
(Jakub Zelenka)

- Hash:
. Fixed bug GH-12936 (hash() function hangs endlessly if using sha512 on
strings >= 4GiB). (nielsdos)
Expand Down
4 changes: 4 additions & 0 deletions ext/ffi/ffi.c
Expand Up @@ -1289,6 +1289,10 @@ static zval *zend_ffi_cdata_write_field(zend_object *obj, zend_string *field_nam
if (cache_slot && *cache_slot == type) {
field = *(cache_slot + 1);
} else {
if (UNEXPECTED(type == NULL)) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign field '%s' to uninitialized FFI\\CData object", ZSTR_VAL(field_name));
return value;
}
if (type->kind == ZEND_FFI_TYPE_POINTER) {
type = ZEND_FFI_TYPE(type->pointer.type);
}
Expand Down
21 changes: 21 additions & 0 deletions ext/ffi/tests/gh9698.phpt
@@ -0,0 +1,21 @@
--TEST--
GH-9698 (stream_wrapper_register crashes with FFI\CData provided as class)
--EXTENSIONS--
ffi
--SKIPIF--
<?php
?>
--FILE--
<?php
try {
stream_wrapper_register('badffi', 'FFI\CData');
file_get_contents('badffi://x');
} catch (Throwable $exception) {
echo $exception->getMessage();
}
?>

DONE
--EXPECT--
Attempt to assign field 'context' to uninitialized FFI\CData object
DONE
6 changes: 6 additions & 0 deletions main/streams/userspace.c
Expand Up @@ -304,6 +304,12 @@ static void user_stream_create_object(struct php_user_stream_wrapper *uwrap, php
add_property_null(object, "context");
}

if (EG(exception) != NULL) {
zval_ptr_dtor(object);
ZVAL_UNDEF(object);
return;
}

if (uwrap->ce->constructor) {
zend_call_known_instance_method_with_0_params(
uwrap->ce->constructor, Z_OBJ_P(object), NULL);
Expand Down

0 comments on commit 40ccc8e

Please sign in to comment.