Skip to content

Commit 1172fc7

Browse files
committed
Promote warnings to exceptions in stream-related functions
1 parent 7d07f19 commit 1172fc7

File tree

8 files changed

+95
-102
lines changed

8 files changed

+95
-102
lines changed

ext/libxml/tests/004.phpt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ $ctxs = array(
1212
new stdclass,
1313
array('a'),
1414
stream_context_create(),
15-
stream_context_create(array('file')),
16-
stream_context_create(array('file' => array('some_opt' => 'aaa')))
1715
);
1816

19-
2017
foreach ($ctxs as $ctx) {
2118
try {
2219
var_dump(libxml_set_streams_context($ctx));
@@ -31,7 +28,6 @@ echo "Done\n";
3128

3229
?>
3330
--EXPECTF--
34-
Warning: stream_context_create(): options should have the form ["wrappername"]["optionname"] = $value in %s004.php on line %d
3531
libxml_set_streams_context() expects parameter 1 to be resource, null given
3632
bool(true)
3733
libxml_set_streams_context() expects parameter 1 to be resource, string given
@@ -44,8 +40,4 @@ libxml_set_streams_context() expects parameter 1 to be resource, array given
4440
bool(true)
4541
NULL
4642
bool(true)
47-
NULL
48-
bool(true)
49-
NULL
50-
bool(true)
5143
Done

ext/standard/streamsfuncs.c

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,8 @@ PHP_FUNCTION(stream_socket_recvfrom)
383383
}
384384

385385
if (to_read <= 0) {
386-
php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0");
387-
RETURN_FALSE;
386+
zend_value_error("Length parameter must be greater than 0");
387+
return;
388388
}
389389

390390
read_buf = zend_string_alloc(to_read, 0);
@@ -493,7 +493,7 @@ PHP_FUNCTION(stream_copy_to_stream)
493493
}
494494
/* }}} */
495495

496-
/* {{{ proto array|false stream_get_meta_data(resource fp)
496+
/* {{{ proto array stream_get_meta_data(resource fp)
497497
Retrieves header/meta data from streams/file pointers */
498498
PHP_FUNCTION(stream_get_meta_data)
499499
{
@@ -784,20 +784,20 @@ PHP_FUNCTION(stream_select)
784784
}
785785

786786
if (!sets) {
787-
php_error_docref(NULL, E_WARNING, "No stream arrays were passed");
788-
RETURN_FALSE;
787+
zend_value_error("No stream arrays were passed");
788+
return;
789789
}
790790

791791
PHP_SAFE_MAX_FD(max_fd, max_set_count);
792792

793793
/* If seconds is not set to null, build the timeval, else we wait indefinitely */
794794
if (!secnull) {
795795
if (sec < 0) {
796-
php_error_docref(NULL, E_WARNING, "The seconds parameter must be greater than 0");
797-
RETURN_FALSE;
796+
zend_value_error("The seconds parameter must be greater than 0");
797+
return;
798798
} else if (usec < 0) {
799-
php_error_docref(NULL, E_WARNING, "The microseconds parameter must be greater than 0");
800-
RETURN_FALSE;
799+
zend_value_error("The microseconds parameter must be greater than 0");
800+
return;
801801
}
802802

803803
/* Windows, Solaris and BSD do not like microsecond values which are >= 1 sec */
@@ -827,7 +827,7 @@ PHP_FUNCTION(stream_select)
827827
retval = php_select(max_fd+1, &rfds, &wfds, &efds, tv_p);
828828

829829
if (retval == -1) {
830-
php_error_docref(NULL, E_WARNING, "unable to select [%d]: %s (max_fd=%d)",
830+
php_error_docref(NULL, E_WARNING, "Unable to select [%d]: %s (max_fd=%d)",
831831
errno, strerror(errno), max_fd);
832832
RETURN_FALSE;
833833
}
@@ -892,7 +892,8 @@ static int parse_context_options(php_stream_context *context, zval *options)
892892
}
893893
} ZEND_HASH_FOREACH_END();
894894
} else {
895-
php_error_docref(NULL, E_WARNING, "options should have the form [\"wrappername\"][\"optionname\"] = $value");
895+
zend_value_error("Options should have the form [\"wrappername\"][\"optionname\"] = $value");
896+
return FAILURE;
896897
}
897898
} ZEND_HASH_FOREACH_END();
898899

@@ -918,9 +919,10 @@ static int parse_context_params(php_stream_context *context, zval *params)
918919
}
919920
if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(params), "options", sizeof("options")-1))) {
920921
if (Z_TYPE_P(tmp) == IS_ARRAY) {
921-
parse_context_options(context, tmp);
922+
return parse_context_options(context, tmp);
922923
} else {
923-
php_error_docref(NULL, E_WARNING, "Invalid stream/context parameter");
924+
zend_value_error("Invalid stream/context parameter");
925+
return FAILURE;
924926
}
925927
}
926928

@@ -957,7 +959,7 @@ static php_stream_context *decode_context_param(zval *contextresource)
957959
}
958960
/* }}} */
959961

960-
/* {{{ proto array|false stream_context_get_options(resource context|resource stream)
962+
/* {{{ proto array stream_context_get_options(resource context|resource stream)
961963
Retrieve options for a stream/wrapper/context */
962964
PHP_FUNCTION(stream_context_get_options)
963965
{
@@ -970,8 +972,8 @@ PHP_FUNCTION(stream_context_get_options)
970972

971973
context = decode_context_param(zcontext);
972974
if (!context) {
973-
php_error_docref(NULL, E_WARNING, "Invalid stream/context parameter");
974-
RETURN_FALSE;
975+
zend_value_error("Invalid stream/context parameter");
976+
return;
975977
}
976978

977979
ZVAL_COPY(return_value, &context->options);
@@ -995,8 +997,8 @@ PHP_FUNCTION(stream_context_set_option)
995997

996998
/* figure out where the context is coming from exactly */
997999
if (!(context = decode_context_param(zcontext))) {
998-
php_error_docref(NULL, E_WARNING, "Invalid stream/context parameter");
999-
RETURN_FALSE;
1000+
zend_value_error("Invalid stream/context parameter");
1001+
return;
10001002
}
10011003

10021004
RETURN_BOOL(parse_context_options(context, options) == SUCCESS);
@@ -1014,8 +1016,8 @@ PHP_FUNCTION(stream_context_set_option)
10141016

10151017
/* figure out where the context is coming from exactly */
10161018
if (!(context = decode_context_param(zcontext))) {
1017-
php_error_docref(NULL, E_WARNING, "Invalid stream/context parameter");
1018-
RETURN_FALSE;
1019+
zend_value_error("Invalid stream/context parameter");
1020+
return;
10191021
}
10201022

10211023
RETURN_BOOL(php_stream_context_set_option(context, wrappername, optionname, zvalue) == SUCCESS);
@@ -1037,15 +1039,15 @@ PHP_FUNCTION(stream_context_set_params)
10371039

10381040
context = decode_context_param(zcontext);
10391041
if (!context) {
1040-
php_error_docref(NULL, E_WARNING, "Invalid stream/context parameter");
1041-
RETURN_FALSE;
1042+
zend_value_error("Invalid stream/context parameter");
1043+
return;
10421044
}
10431045

10441046
RETVAL_BOOL(parse_context_params(context, params) == SUCCESS);
10451047
}
10461048
/* }}} */
10471049

1048-
/* {{{ proto array|false stream_context_get_params(resource context|resource stream)
1050+
/* {{{ proto array stream_context_get_params(resource context|resource stream)
10491051
Get parameters of a file context */
10501052
PHP_FUNCTION(stream_context_get_params)
10511053
{
@@ -1058,8 +1060,8 @@ PHP_FUNCTION(stream_context_get_params)
10581060

10591061
context = decode_context_param(zcontext);
10601062
if (!context) {
1061-
php_error_docref(NULL, E_WARNING, "Invalid stream/context parameter");
1062-
RETURN_FALSE;
1063+
zend_value_error("Invalid stream/context parameter");
1064+
return;
10631065
}
10641066

10651067
array_init(return_value);
@@ -1090,7 +1092,9 @@ PHP_FUNCTION(stream_context_get_default)
10901092
context = FG(default_context);
10911093

10921094
if (params) {
1093-
parse_context_options(context, params);
1095+
if (parse_context_options(context, params) == FAILURE) {
1096+
return;
1097+
}
10941098
}
10951099

10961100
php_stream_context_to_zval(context, return_value);
@@ -1113,7 +1117,9 @@ PHP_FUNCTION(stream_context_set_default)
11131117
}
11141118
context = FG(default_context);
11151119

1116-
parse_context_options(context, options);
1120+
if (parse_context_options(context, options) == FAILURE) {
1121+
return;
1122+
}
11171123

11181124
php_stream_context_to_zval(context, return_value);
11191125
}
@@ -1255,8 +1261,8 @@ PHP_FUNCTION(stream_filter_remove)
12551261

12561262
filter = zend_fetch_resource(Z_RES_P(zfilter), NULL, php_file_le_stream_filter());
12571263
if (!filter) {
1258-
php_error_docref(NULL, E_WARNING, "Invalid resource given, not a stream filter");
1259-
RETURN_FALSE;
1264+
zend_value_error("Invalid resource given, not a stream filter");
1265+
return;
12601266
}
12611267

12621268
if (php_stream_filter_flush(filter, 1) == FAILURE) {
@@ -1293,8 +1299,8 @@ PHP_FUNCTION(stream_get_line)
12931299
ZEND_PARSE_PARAMETERS_END();
12941300

12951301
if (max_length < 0) {
1296-
php_error_docref(NULL, E_WARNING, "The maximum allowed length must be greater than or equal to zero");
1297-
RETURN_FALSE;
1302+
zend_value_error("The maximum allowed length must be greater than or equal to zero");
1303+
return;
12981304
}
12991305
if (!max_length) {
13001306
max_length = PHP_SOCK_CHUNK_SIZE;
@@ -1429,16 +1435,16 @@ PHP_FUNCTION(stream_set_chunk_size)
14291435
ZEND_PARSE_PARAMETERS_END();
14301436

14311437
if (csize <= 0) {
1432-
php_error_docref(NULL, E_WARNING, "The chunk size must be a positive integer, given " ZEND_LONG_FMT, csize);
1433-
RETURN_FALSE;
1438+
zend_value_error("The chunk size must be a positive integer, given " ZEND_LONG_FMT, csize);
1439+
return;
14341440
}
14351441
/* stream.chunk_size is actually a size_t, but php_stream_set_option
14361442
* can only use an int to accept the new value and return the old one.
14371443
* In any case, values larger than INT_MAX for a chunk size make no sense.
14381444
*/
14391445
if (csize > INT_MAX) {
1440-
php_error_docref(NULL, E_WARNING, "The chunk size cannot be larger than %d", INT_MAX);
1441-
RETURN_FALSE;
1446+
zend_value_error("The chunk size cannot be larger than %d", INT_MAX);
1447+
return;
14421448
}
14431449

14441450
php_stream_from_zval(stream, zstream);
@@ -1504,8 +1510,8 @@ PHP_FUNCTION(stream_socket_enable_crypto)
15041510
zval *val;
15051511

15061512
if (!GET_CTX_OPT(stream, "ssl", "crypto_method", val)) {
1507-
php_error_docref(NULL, E_WARNING, "When enabling encryption you must specify the crypto type");
1508-
RETURN_FALSE;
1513+
zend_value_error("When enabling encryption you must specify the crypto type");
1514+
return;
15091515
}
15101516

15111517
cryptokind = Z_LVAL_P(val);
@@ -1679,7 +1685,7 @@ PHP_FUNCTION(sapi_windows_vt100_support)
16791685
"%s() was not able to analyze the specified stream",
16801686
get_active_function_name()
16811687
);
1682-
RETURN_FALSE;
1688+
return;
16831689
}
16841690

16851691
/* Check if the file descriptor is a console */

ext/standard/tests/file/userstreams_002.phpt

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function test($name, $fd, $return_value) {
2424
$w = $e = null;
2525
try {
2626
var_dump(stream_select($r, $w, $e, 0) !== false);
27-
} catch (TypeError $e) {
27+
} catch (TypeError|ValueError $e) {
2828
echo $e->getMessage(), "\n";
2929
}
3030
}
@@ -55,34 +55,26 @@ bool(true)
5555
Warning: stream_select(): test_wrapper_base::stream_cast is not implemented! in %s
5656

5757
Warning: stream_select(): cannot represent a stream of type user-space as a select()able descriptor in %s
58-
59-
Warning: stream_select(): No stream arrays were passed in %s
60-
bool(false)
58+
No stream arrays were passed
6159

6260
------ return value is false: -------
6361

6462
Warning: stream_select(): cannot represent a stream of type user-space as a select()able descriptor in %s
65-
66-
Warning: stream_select(): No stream arrays were passed in %s
67-
bool(false)
63+
No stream arrays were passed
6864

6965
------ return value not a stream resource: -------
7066

7167
Warning: stream_select(): test_wrapper::stream_cast must return a stream resource in %s
7268

7369
Warning: stream_select(): cannot represent a stream of type user-space as a select()able descriptor in %s
74-
75-
Warning: stream_select(): No stream arrays were passed in %s
76-
stream_select(): supplied argument is not a valid stream resource
70+
No stream arrays were passed
7771

7872
------ return value is stream itself: -------
7973

8074
Warning: stream_select(): test_wrapper::stream_cast must not return itself in %s
8175

8276
Warning: stream_select(): cannot represent a stream of type user-space as a select()able descriptor in %s
83-
84-
Warning: stream_select(): No stream arrays were passed in %s
85-
bool(false)
77+
No stream arrays were passed
8678

8779
------ return value cannot be casted: -------
8880

@@ -91,6 +83,4 @@ Warning: stream_select(): test_wrapper_base::stream_cast is not implemented! in
9183
Warning: stream_select(): cannot represent a stream of type user-space as a select()able descriptor in %s
9284

9385
Warning: stream_select(): cannot represent a stream of type user-space as a select()able descriptor in %s
94-
95-
Warning: stream_select(): No stream arrays were passed in %s
96-
bool(false)
86+
No stream arrays were passed

ext/standard/tests/filters/stream_filter_remove_error.phpt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,20 @@ $filter = stream_filter_append( $fp, "string.rot13", STREAM_FILTER_WRITE );
2121
echo "*** Testing stream_filter_remove() : error conditions ***\n";
2222

2323
echo "\n-- Testing stream_filter_remove() function with bad resource --\n";
24-
var_dump( stream_filter_remove( $fp ) );
24+
try {
25+
stream_filter_remove($fp);
26+
} catch (ValueError $exception) {
27+
echo $exception->getMessage() . "\n";
28+
}
2529

2630
echo "\n-- Testing stream_filter_remove() function with an already removed filter --\n";
2731
// Double remove it
28-
var_dump( stream_filter_remove( $filter ) );
29-
var_dump( stream_filter_remove( $filter ) );
32+
var_dump(stream_filter_remove( $filter ));
33+
try {
34+
stream_filter_remove($filter);
35+
} catch (ValueError $exception) {
36+
echo $exception->getMessage() . "\n";
37+
}
3038

3139
fclose( $fp );
3240

@@ -38,16 +46,12 @@ $file = __DIR__ . DIRECTORY_SEPARATOR . 'streamfilterTest.txt';
3846
unlink( $file );
3947

4048
?>
41-
--EXPECTF--
49+
--EXPECT--
4250
*** Testing stream_filter_remove() : error conditions ***
4351

4452
-- Testing stream_filter_remove() function with bad resource --
45-
46-
Warning: stream_filter_remove(): Invalid resource given, not a stream filter in %s on line %d
47-
bool(false)
53+
Invalid resource given, not a stream filter
4854

4955
-- Testing stream_filter_remove() function with an already removed filter --
5056
bool(true)
51-
52-
Warning: stream_filter_remove(): Invalid resource given, not a stream filter in %s on line %d
53-
bool(false)
57+
Invalid resource given, not a stream filter

ext/standard/tests/streams/bug44712.phpt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ bug#44712 (stream_context_set_params segfaults on invalid arguments)
33
--FILE--
44
<?php
55
$ctx = stream_context_get_default();
6-
stream_context_set_params($ctx, array("options" => 1));
6+
try {
7+
stream_context_set_params($ctx, array("options" => 1));
8+
} catch (ValueError $exception) {
9+
echo $exception->getMessage() . "\n";
10+
}
711
?>
8-
--EXPECTF--
9-
Warning: stream_context_set_params(): Invalid stream/context parameter in %sbug44712.php on line %d
12+
--EXPECT--
13+
Invalid stream/context parameter

ext/standard/tests/streams/bug71884.phpt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ Bug #71884 (Null pointer deref (segfault) in stream_context_get_default)
44
<?php
55
$arr=array();
66
$arr[0]['A']=0;
7-
stream_context_get_default($arr);
7+
try {
8+
stream_context_get_default($arr);
9+
} catch (ValueError $exception) {
10+
echo $exception->getMessage() . "\n";
11+
}
812
?>
9-
--EXPECTF--
10-
Warning: stream_context_get_default(): options should have the form ["wrappername"]["optionname"] = $value in %sbug71884.php on line %d
13+
--EXPECT--
14+
Options should have the form ["wrappername"]["optionname"] = $value

0 commit comments

Comments
 (0)