Skip to content

Add dedicated StreamBucket class #13111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ $standard = new ReflectionExtension('standard');
var_dump($standard->getClassNames());
?>
--EXPECT--
array(4) {
array(5) {
[0]=>
string(22) "__PHP_Incomplete_Class"
[1]=>
string(14) "AssertionError"
[2]=>
string(15) "php_user_filter"
[3]=>
string(12) "StreamBucket"
[4]=>
string(9) "Directory"
}
8 changes: 4 additions & 4 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -3744,19 +3744,19 @@ function get_headers(string $url, bool $associative = false, $context = null): a
* @param resource $brigade
* @refcount 1
*/
function stream_bucket_make_writeable($brigade): ?object {}
function stream_bucket_make_writeable($brigade): ?StreamBucket {}

/** @param resource $brigade */
function stream_bucket_prepend($brigade, object $bucket): void {}
function stream_bucket_prepend($brigade, StreamBucket $bucket): void {}

/** @param resource $brigade */
function stream_bucket_append($brigade, object $bucket): void {}
function stream_bucket_append($brigade, StreamBucket $bucket): void {}

/**
* @param resource $stream
* @refcount 1
*/
function stream_bucket_new($stream, string $buffer): object {}
function stream_bucket_new($stream, string $buffer): StreamBucket {}

/**
* @return array<int, string>
Expand Down
8 changes: 4 additions & 4 deletions ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion ext/standard/tests/file/bug39551.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Bug #39551 (Segfault with stream_bucket_new in user filter)
<?php

$bucket = stream_bucket_new(fopen('php://temp', 'w+'), '');
var_dump($bucket);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you're really only interested in the class, perhaps use:

echo get_class($bucket), "\n";

Then you don't have to update the test later when bucket becomes an object, or datalen becomes dataLength.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good idea if there were other tests which ensured the structure of StreamBucket. But I'd prefer to keep this test as-is in this case, since that's the only place where the properties are displayed.


class bucketFilter extends php_user_filter {
public function filter($in, $out, &$consumed, $closing ): int {
Expand All @@ -20,5 +21,15 @@ stream_get_contents($s);

echo "Done\n";
?>
--EXPECT--
--EXPECTF--
object(StreamBucket)#%d (%d) {
["bucket"]=>
resource(%d) of type (userfilter.bucket)
["data"]=>
string(0) ""
["datalen"]=>
int(0)
["dataLength"]=>
int(0)
}
Done
40 changes: 23 additions & 17 deletions ext/standard/user_filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ PHP_METHOD(php_user_filter, onClose)
}

static zend_class_entry *user_filter_class_entry;
static zend_class_entry *stream_bucket_class_entry;

static ZEND_RSRC_DTOR_FUNC(php_bucket_dtor)
{
Expand All @@ -75,6 +76,7 @@ PHP_MINIT_FUNCTION(user_filters)
{
/* init the filter class ancestor */
user_filter_class_entry = register_class_php_user_filter();
stream_bucket_class_entry = register_class_StreamBucket();

/* Filters will dispose of their brigades */
le_bucket_brigade = zend_register_list_destructors_ex(NULL, NULL, PHP_STREAM_BRIGADE_RES_NAME, module_number);
Expand Down Expand Up @@ -351,16 +353,17 @@ PHP_FUNCTION(stream_bucket_make_writeable)
RETURN_THROWS();
}

ZVAL_NULL(return_value);

if (brigade->head && (bucket = php_stream_bucket_make_writeable(brigade->head))) {
ZVAL_RES(&zbucket, zend_register_resource(bucket, le_bucket));
object_init(return_value);
add_property_zval(return_value, "bucket", &zbucket);
object_init_ex(return_value, stream_bucket_class_entry);
zend_update_property(Z_OBJCE_P(return_value), Z_OBJ_P(return_value), ZEND_STRL("bucket"), &zbucket);
/* add_property_zval increments the refcount which is unwanted here */
zval_ptr_dtor(&zbucket);
add_property_stringl(return_value, "data", bucket->buf, bucket->buflen);
add_property_long(return_value, "datalen", bucket->buflen);
zend_update_property_stringl(Z_OBJCE_P(return_value), Z_OBJ_P(return_value), ZEND_STRL("data"), bucket->buf, bucket->buflen);
zend_update_property_long(Z_OBJCE_P(return_value), Z_OBJ_P(return_value), ZEND_STRL("datalen"), bucket->buflen);
zend_update_property_long(Z_OBJCE_P(return_value), Z_OBJ_P(return_value), ZEND_STRL("dataLength"), bucket->buflen);
} else {
ZVAL_NULL(return_value);
}
}
/* }}} */
Expand All @@ -369,30 +372,32 @@ PHP_FUNCTION(stream_bucket_make_writeable)
static void php_stream_bucket_attach(int append, INTERNAL_FUNCTION_PARAMETERS)
{
zval *zbrigade, *zobject;
zval *pzbucket, *pzdata;
zval *pzbucket, *pzdata, rv;
php_stream_bucket_brigade *brigade;
php_stream_bucket *bucket;

ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_RESOURCE(zbrigade)
Z_PARAM_OBJECT(zobject)
Z_PARAM_OBJECT_OF_CLASS(zobject, stream_bucket_class_entry)
ZEND_PARSE_PARAMETERS_END();

if (NULL == (pzbucket = zend_hash_str_find_deref(Z_OBJPROP_P(zobject), "bucket", sizeof("bucket")-1))) {
zend_argument_value_error(2, "must be an object that has a \"bucket\" property");
if ((brigade = (php_stream_bucket_brigade*)zend_fetch_resource(
Z_RES_P(zbrigade), PHP_STREAM_BRIGADE_RES_NAME, le_bucket_brigade)) == NULL) {
RETURN_THROWS();
}

if ((brigade = (php_stream_bucket_brigade*)zend_fetch_resource(
Z_RES_P(zbrigade), PHP_STREAM_BRIGADE_RES_NAME, le_bucket_brigade)) == NULL) {
if (NULL == (pzbucket = zend_read_property(NULL, Z_OBJ_P(zobject), "bucket", sizeof("bucket")-1, false, &rv))) {
zend_argument_value_error(2, "must be an object that has a \"bucket\" property");
RETURN_THROWS();
}
ZVAL_DEREF(pzbucket);

if ((bucket = (php_stream_bucket *)zend_fetch_resource_ex(pzbucket, PHP_STREAM_BUCKET_RES_NAME, le_bucket)) == NULL) {
RETURN_THROWS();
}

if (NULL != (pzdata = zend_hash_str_find_deref(Z_OBJPROP_P(zobject), "data", sizeof("data")-1)) && Z_TYPE_P(pzdata) == IS_STRING) {
if (NULL != (pzdata = zend_read_property(NULL, Z_OBJ_P(zobject), "data", sizeof("data")-1, false, &rv))) {
ZVAL_DEREF(pzdata);
if (!bucket->own_buf) {
bucket = php_stream_bucket_make_writeable(bucket);
}
Expand Down Expand Up @@ -454,12 +459,13 @@ PHP_FUNCTION(stream_bucket_new)
bucket = php_stream_bucket_new(stream, pbuffer, buffer_len, 1, php_stream_is_persistent(stream));

ZVAL_RES(&zbucket, zend_register_resource(bucket, le_bucket));
object_init(return_value);
add_property_zval(return_value, "bucket", &zbucket);
object_init_ex(return_value, stream_bucket_class_entry);
zend_update_property(Z_OBJCE_P(return_value), Z_OBJ_P(return_value), ZEND_STRL("bucket"), &zbucket);
/* add_property_zval increments the refcount which is unwanted here */
zval_ptr_dtor(&zbucket);
add_property_stringl(return_value, "data", bucket->buf, bucket->buflen);
add_property_long(return_value, "datalen", bucket->buflen);
zend_update_property_stringl(Z_OBJCE_P(return_value), Z_OBJ_P(return_value), ZEND_STRL("data"), bucket->buf, bucket->buflen);
zend_update_property_long(Z_OBJCE_P(return_value), Z_OBJ_P(return_value), ZEND_STRL("datalen"), bucket->buflen);
zend_update_property_long(Z_OBJCE_P(return_value), Z_OBJ_P(return_value), ZEND_STRL("dataLength"), bucket->buflen);
}
/* }}} */

Expand Down
15 changes: 15 additions & 0 deletions ext/standard/user_filters.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,18 @@ public function onCreate(): bool {}
/** @tentative-return-type */
public function onClose(): void {}
}

final class StreamBucket
{
/**
* @var resource
* @readonly
*/
public $bucket;
/** @readonly */
public string $data;
/** @readonly */
public int $datalen;
/** @readonly */
public int $dataLength;
}
41 changes: 40 additions & 1 deletion ext/standard/user_filters_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading