Skip to content

Commit ed6e289

Browse files
kocsismatenielsdos
andauthored
Make the return type of some stream context related functions true (#12720)
Co-authored-by: Niels Dossche <[email protected]>
1 parent c15db22 commit ed6e289

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

ext/standard/basic_functions.stub.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3320,7 +3320,7 @@ function stream_select(?array &$read, ?array &$write, ?array &$except, ?int $sec
33203320
function stream_context_create(?array $options = null, ?array $params = null) {}
33213321

33223322
/** @param resource $context */
3323-
function stream_context_set_params($context, array $params): bool {}
3323+
function stream_context_set_params($context, array $params): true {}
33243324

33253325
/**
33263326
* @param resource $context
@@ -3330,10 +3330,10 @@ function stream_context_set_params($context, array $params): bool {}
33303330
function stream_context_get_params($context): array {}
33313331

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

33353335
/** @param resource $context */
3336-
function stream_context_set_options($context, array $options): bool {}
3336+
function stream_context_set_options($context, array $options): true {}
33373337

33383338
/**
33393339
* @param resource $stream_or_context

ext/standard/basic_functions_arginfo.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/streamsfuncs.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -908,11 +908,10 @@ static void user_space_stream_notifier_dtor(php_stream_notifier *notifier)
908908
}
909909
}
910910

911-
static int parse_context_options(php_stream_context *context, HashTable *options)
911+
static zend_result parse_context_options(php_stream_context *context, HashTable *options)
912912
{
913913
zval *wval, *oval;
914914
zend_string *wkey, *okey;
915-
int ret = SUCCESS;
916915

917916
ZEND_HASH_FOREACH_STR_KEY_VAL(options, wkey, wval) {
918917
ZVAL_DEREF(wval);
@@ -930,12 +929,11 @@ static int parse_context_options(php_stream_context *context, HashTable *options
930929
}
931930
} ZEND_HASH_FOREACH_END();
932931

933-
return ret;
932+
return SUCCESS;
934933
}
935934

936-
static int parse_context_params(php_stream_context *context, HashTable *params)
935+
static zend_result parse_context_params(php_stream_context *context, HashTable *params)
937936
{
938-
int ret = SUCCESS;
939937
zval *tmp;
940938

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

962-
return ret;
960+
return SUCCESS;
963961
}
964962

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

1051-
RETURN_BOOL(parse_context_options(context, options) == SUCCESS);
1049+
if (parse_context_options(context, options) == FAILURE) {
1050+
RETURN_THROWS();
1051+
}
1052+
1053+
RETURN_TRUE;
10521054
} else {
10531055
if (!optionname) {
10541056
zend_argument_value_error(3, "cannot be null when argument #2 ($wrapper_or_options) is a string");
@@ -1081,7 +1083,11 @@ PHP_FUNCTION(stream_context_set_options)
10811083
RETURN_THROWS();
10821084
}
10831085

1084-
RETURN_BOOL(parse_context_options(context, options) == SUCCESS);
1086+
if (parse_context_options(context, options) == FAILURE) {
1087+
RETURN_THROWS();
1088+
}
1089+
1090+
RETURN_TRUE;
10851091
}
10861092

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

1105-
RETVAL_BOOL(parse_context_params(context, params) == SUCCESS);
1111+
if (parse_context_params(context, params) == FAILURE) {
1112+
RETURN_THROWS();
1113+
}
1114+
1115+
RETURN_TRUE;
11061116
}
11071117
/* }}} */
11081118

@@ -1197,11 +1207,15 @@ PHP_FUNCTION(stream_context_create)
11971207
context = php_stream_context_alloc();
11981208

11991209
if (options) {
1200-
parse_context_options(context, options);
1210+
if (parse_context_options(context, options) == FAILURE) {
1211+
RETURN_THROWS();
1212+
}
12011213
}
12021214

12031215
if (params) {
1204-
parse_context_params(context, params);
1216+
if (parse_context_params(context, params) == FAILURE) {
1217+
RETURN_THROWS();
1218+
}
12051219
}
12061220

12071221
RETURN_RES(context->res);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Test the error cases of stream_context_create()
3+
--FILE--
4+
<?php
5+
try {
6+
stream_context_create(['ssl' => "abc"]);
7+
} catch (ValueError $exception) {
8+
echo $exception->getMessage() . "\n";
9+
}
10+
11+
try {
12+
stream_context_create(['ssl' => ['verify_peer'=> false]], ["options" => ['ssl' => "abc"]]);
13+
} catch (ValueError $exception) {
14+
echo $exception->getMessage() . "\n";
15+
}
16+
17+
try {
18+
stream_context_create(['ssl' => ['verify_peer'=> false]], ["options" => false]);
19+
} catch (TypeError $exception) {
20+
echo $exception->getMessage() . "\n";
21+
}
22+
?>
23+
--EXPECT--
24+
Options should have the form ["wrappername"]["optionname"] = $value
25+
Options should have the form ["wrappername"]["optionname"] = $value
26+
Invalid stream/context parameter

0 commit comments

Comments
 (0)