Skip to content

Commit

Permalink
Make the return type of some stream context related functions true (#…
Browse files Browse the repository at this point in the history
…12720)

Co-authored-by: Niels Dossche <[email protected]>
  • Loading branch information
kocsismate and nielsdos authored Nov 19, 2023
1 parent c15db22 commit ed6e289
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
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
26 changes: 26 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,26 @@
--TEST--
Test the error cases of stream_context_create()
--FILE--
<?php
try {
stream_context_create(['ssl' => "abc"]);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

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

try {
stream_context_create(['ssl' => ['verify_peer'=> false]], ["options" => false]);
} catch (TypeError $exception) {
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

0 comments on commit ed6e289

Please sign in to comment.