Skip to content

Make the return type of some stream context related functions true #12720

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 3 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -3320,7 +3320,7 @@ function stream_select(?array &$read, ?array &$write, ?array &$except, ?int $sec
function stream_context_create(?array $options = null, ?array $params = null) {}

/** @param resource $context */
function stream_context_set_params($context, array $params): bool {}
function stream_context_set_params($context, array $params): true {}

/**
* @param resource $context
Expand All @@ -3330,10 +3330,10 @@ function stream_context_set_params($context, array $params): bool {}
function stream_context_get_params($context): array {}

/** @param resource $context */
function stream_context_set_option($context, array|string $wrapper_or_options, ?string $option_name = null, mixed $value = UNKNOWN): bool {}
function stream_context_set_option($context, array|string $wrapper_or_options, ?string $option_name = null, mixed $value = UNKNOWN): true {}

/** @param resource $context */
function stream_context_set_options($context, array $options): bool {}
function stream_context_set_options($context, array $options): true {}

/**
* @param resource $stream_or_context
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.

36 changes: 25 additions & 11 deletions ext/standard/streamsfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -908,11 +908,10 @@ static void user_space_stream_notifier_dtor(php_stream_notifier *notifier)
}
}

static int parse_context_options(php_stream_context *context, HashTable *options)
static zend_result parse_context_options(php_stream_context *context, HashTable *options)
{
zval *wval, *oval;
zend_string *wkey, *okey;
int ret = SUCCESS;

ZEND_HASH_FOREACH_STR_KEY_VAL(options, wkey, wval) {
ZVAL_DEREF(wval);
Expand All @@ -930,12 +929,11 @@ static int parse_context_options(php_stream_context *context, HashTable *options
}
} ZEND_HASH_FOREACH_END();

return ret;
return SUCCESS;
}

static int parse_context_params(php_stream_context *context, HashTable *params)
static zend_result parse_context_params(php_stream_context *context, HashTable *params)
{
int ret = SUCCESS;
zval *tmp;

if (NULL != (tmp = zend_hash_str_find(params, "notification", sizeof("notification")-1))) {
Expand All @@ -959,7 +957,7 @@ static int parse_context_params(php_stream_context *context, HashTable *params)
}
}

return ret;
return SUCCESS;
}

/* given a zval which is either a stream or a context, return the underlying
Expand Down Expand Up @@ -1048,7 +1046,11 @@ PHP_FUNCTION(stream_context_set_option)
RETURN_THROWS();
}

RETURN_BOOL(parse_context_options(context, options) == SUCCESS);
if (parse_context_options(context, options) == FAILURE) {
RETURN_THROWS();
}

RETURN_TRUE;
} else {
if (!optionname) {
zend_argument_value_error(3, "cannot be null when argument #2 ($wrapper_or_options) is a string");
Expand Down Expand Up @@ -1081,7 +1083,11 @@ PHP_FUNCTION(stream_context_set_options)
RETURN_THROWS();
}

RETURN_BOOL(parse_context_options(context, options) == SUCCESS);
if (parse_context_options(context, options) == FAILURE) {
RETURN_THROWS();
}

RETURN_TRUE;
}

/* {{{ Set parameters for a file context */
Expand All @@ -1102,7 +1108,11 @@ PHP_FUNCTION(stream_context_set_params)
RETURN_THROWS();
}

RETVAL_BOOL(parse_context_params(context, params) == SUCCESS);
if (parse_context_params(context, params) == FAILURE) {
RETURN_THROWS();
}

RETURN_TRUE;
}
/* }}} */

Expand Down Expand Up @@ -1197,11 +1207,15 @@ PHP_FUNCTION(stream_context_create)
context = php_stream_context_alloc();

if (options) {
parse_context_options(context, options);
if (parse_context_options(context, options) == FAILURE) {
RETURN_THROWS();
}
}

if (params) {
parse_context_params(context, params);
if (parse_context_params(context, params) == FAILURE) {
RETURN_THROWS();
}
}

RETURN_RES(context->res);
Expand Down
27 changes: 27 additions & 0 deletions ext/standard/tests/streams/stream_context_create_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Test the error cases of stream_context_create()
--FILE--
<?php
$arr = [];
try {
stream_context_create($arr);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
stream_context_create(['ssl' => ['verify_peer'=> false]], ["options" => $arr]);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
stream_context_create(['ssl' => ['verify_peer'=> false]], ["options" => false]);
} catch (ValueError $exception) {
Copy link
Member

Choose a reason for hiding this comment

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

Should be a TypeError

echo $exception->getMessage() . "\n";
}
?>
--EXPECT--
Options should have the form ["wrappername"]["optionname"] = $value
Options should have the form ["wrappername"]["optionname"] = $value
Invalid stream/context parameter