Skip to content

Commit f19950f

Browse files
kocsismatenikic
authored andcommitted
Promote some warnings to exceptions in standard lib
Closes GH-4879.
1 parent 11ce417 commit f19950f

File tree

8 files changed

+74
-74
lines changed

8 files changed

+74
-74
lines changed

ext/standard/basic_functions.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "php_getopt.h"
2929
#include "ext/standard/info.h"
3030
#include "ext/session/php_session.h"
31+
#include "zend_exceptions.h"
3132
#include "zend_operators.h"
3233
#include "ext/standard/php_dns.h"
3334
#include "ext/standard/php_uuencode.h"
@@ -2598,10 +2599,10 @@ PHP_FUNCTION(putenv)
25982599
Z_PARAM_STRING(setting, setting_len)
25992600
ZEND_PARSE_PARAMETERS_END();
26002601

2601-
if(setting_len == 0 || setting[0] == '=') {
2602-
php_error_docref(NULL, E_WARNING, "Invalid parameter syntax");
2603-
RETURN_FALSE;
2604-
}
2602+
if (setting_len == 0 || setting[0] == '=') {
2603+
zend_value_error("Invalid parameter syntax");
2604+
return;
2605+
}
26052606

26062607
pe.putenv_string = estrndup(setting, setting_len);
26072608
pe.key = estrndup(setting, setting_len);
@@ -2982,8 +2983,8 @@ PHP_FUNCTION(sleep)
29822983
ZEND_PARSE_PARAMETERS_END();
29832984

29842985
if (num < 0) {
2985-
php_error_docref(NULL, E_WARNING, "Number of seconds must be greater than or equal to 0");
2986-
RETURN_FALSE;
2986+
zend_value_error("Number of seconds must be greater than or equal to 0");
2987+
return;
29872988
}
29882989
#ifdef PHP_SLEEP_NON_VOID
29892990
RETURN_LONG(php_sleep((unsigned int)num));
@@ -3006,8 +3007,8 @@ PHP_FUNCTION(usleep)
30063007
ZEND_PARSE_PARAMETERS_END();
30073008

30083009
if (num < 0) {
3009-
php_error_docref(NULL, E_WARNING, "Number of microseconds must be greater than or equal to 0");
3010-
RETURN_FALSE;
3010+
zend_value_error("Number of microseconds must be greater than or equal to 0");
3011+
return;
30113012
}
30123013
if (usleep((unsigned int)num) < 0) {
30133014
#if ZEND_DEBUG
@@ -3033,12 +3034,12 @@ PHP_FUNCTION(time_nanosleep)
30333034
ZEND_PARSE_PARAMETERS_END();
30343035

30353036
if (tv_sec < 0) {
3036-
php_error_docref(NULL, E_WARNING, "The seconds value must be greater than 0");
3037-
RETURN_FALSE;
3037+
zend_value_error("The seconds value must be greater than 0");
3038+
return;
30383039
}
30393040
if (tv_nsec < 0) {
3040-
php_error_docref(NULL, E_WARNING, "The nanoseconds value must be greater than 0");
3041-
RETURN_FALSE;
3041+
zend_value_error("The nanoseconds value must be greater than 0");
3042+
return;
30423043
}
30433044

30443045
php_req.tv_sec = (time_t) tv_sec;
@@ -3051,7 +3052,8 @@ PHP_FUNCTION(time_nanosleep)
30513052
add_assoc_long_ex(return_value, "nanoseconds", sizeof("nanoseconds")-1, php_rem.tv_nsec);
30523053
return;
30533054
} else if (errno == EINVAL) {
3054-
php_error_docref(NULL, E_WARNING, "nanoseconds was not in the range 0 to 999 999 999 or seconds was negative");
3055+
zend_value_error("Nanoseconds was not in the range 0 to 999 999 999 or seconds was negative");
3056+
return;
30553057
}
30563058

30573059
RETURN_FALSE;
@@ -4348,7 +4350,7 @@ PHP_FUNCTION(is_uploaded_file)
43484350
}
43494351

43504352
ZEND_PARSE_PARAMETERS_START(1, 1)
4351-
Z_PARAM_STRING(path, path_len)
4353+
Z_PARAM_PATH(path, path_len)
43524354
ZEND_PARSE_PARAMETERS_END();
43534355

43544356
if (zend_hash_str_exists(SG(rfc1867_uploaded_files), path, path_len)) {
@@ -4583,6 +4585,8 @@ PHP_FUNCTION(parse_ini_string)
45834585
* is not the same as ini_get_all() which returns only registered ini options. Only useful for devs to debug php.ini scanner/parser! */
45844586
PHP_FUNCTION(config_get_hash) /* {{{ */
45854587
{
4588+
ZEND_PARSE_PARAMETERS_NONE();
4589+
45864590
HashTable *hash = php_ini_get_configuration_hash();
45874591

45884592
array_init(return_value);

ext/standard/tests/general_functions/putenv.phpt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,17 @@ var_dump(getenv($var_name));
1515
var_dump(putenv($var_name));
1616
var_dump(getenv($var_name));
1717

18-
var_dump(putenv("=123"));
19-
var_dump(putenv(""));
18+
try {
19+
putenv("=123");
20+
} catch (ValueError $exception) {
21+
echo $exception->getMessage() . "\n";
22+
}
23+
24+
try {
25+
putenv("");
26+
} catch (ValueError $exception) {
27+
echo $exception->getMessage() . "\n";
28+
}
2029

2130
echo "Done\n";
2231
?>
@@ -28,10 +37,6 @@ bool(true)
2837
string(0) ""
2938
bool(true)
3039
bool(false)
31-
32-
Warning: putenv(): Invalid parameter syntax in %s on line %d
33-
bool(false)
34-
35-
Warning: putenv(): Invalid parameter syntax in %s on line %d
36-
bool(false)
40+
Invalid parameter syntax
41+
Invalid parameter syntax
3742
Done

ext/standard/tests/general_functions/sleep_error.phpt

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,13 @@
22
Test sleep() function : error conditions
33
--FILE--
44
<?php
5-
/* Prototype : int sleep ( int $seconds )
6-
* Description: Delays the program execution for the given number of seconds .
7-
* Source code: ext/standard/basic_functions.c
8-
*/
9-
set_time_limit(20);
105

11-
echo "*** Testing sleep() : error conditions ***\n";
12-
13-
echo "\n-- Testing sleep() function with negative interval --\n";
14-
$seconds = -10;
15-
var_dump( sleep($seconds) );
6+
sleep(-10);
167

178
?>
18-
===DONE===
199
--EXPECTF--
20-
*** Testing sleep() : error conditions ***
21-
22-
-- Testing sleep() function with negative interval --
23-
24-
Warning: sleep(): Number of seconds must be greater than or equal to 0 in %s on line %d
25-
bool(false)
26-
===DONE===
10+
Fatal error: Uncaught ValueError: Number of seconds must be greater than or equal to 0 in %s
11+
Stack trace:
12+
#0 %s(%d): sleep(-10)
13+
#1 {main}
14+
thrown in %s on line %d

ext/standard/tests/general_functions/usleep_error.phpt

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,13 @@
22
Test usleep() function : error conditions
33
--FILE--
44
<?php
5-
/* Prototype : void usleep ( int $micro_seconds )
6-
* Description: Delays program execution for the given number of micro seconds.
7-
* Source code: ext/standard/basic_functions.c
8-
*/
95

10-
set_time_limit(20);
11-
12-
echo "*** Testing usleep() : error conditions ***\n";
13-
14-
echo "\n-- Testing usleep() function with negative interval --\n";
15-
$seconds = -10;
16-
var_dump( usleep($seconds) );
6+
usleep(-10);
177

188
?>
19-
===DONE===
209
--EXPECTF--
21-
*** Testing usleep() : error conditions ***
22-
23-
-- Testing usleep() function with negative interval --
24-
25-
Warning: usleep(): Number of microseconds must be greater than or equal to 0 in %s on line %d
26-
bool(false)
27-
===DONE===
10+
Fatal error: Uncaught ValueError: Number of microseconds must be greater than or equal to 0 in %s
11+
Stack trace:
12+
#0 %s(%d): usleep(-10)
13+
#1 {main}
14+
thrown in %s on line %d

ext/standard/tests/misc/time_nanosleep_error3.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ if (!function_exists('time_nanosleep')) die("skip");
99
--FILE--
1010
<?php
1111

12-
$nano = time_nanosleep(-2, 1000);
12+
time_nanosleep(-2, 1000);
1313

1414
?>
1515
--EXPECTF--
16-
Warning: time_nanosleep(): The seconds value must be greater than 0 in %stime_nanosleep_error3.php on line %d
16+
Fatal error: Uncaught ValueError: The seconds value must be greater than 0 in %s:%d
17+
Stack trace:
18+
#0 %s(%d): time_nanosleep(-2, 1000)
19+
#1 {main}
20+
thrown in %s on line %d

ext/standard/tests/misc/time_nanosleep_error4.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ if (!function_exists('time_nanosleep')) die("skip");
1010
--FILE--
1111
<?php
1212

13-
$nano = time_nanosleep(0, -10);
13+
time_nanosleep(0, -10);
1414

1515
?>
1616
--EXPECTF--
17-
Warning: time_nanosleep(): The nanoseconds value must be greater than 0 in %stime_nanosleep_error4.php on line %d
17+
Fatal error: Uncaught ValueError: The nanoseconds value must be greater than 0 in %s:%d
18+
Stack trace:
19+
#0 %s(%d): time_nanosleep(0, -10)
20+
#1 {main}
21+
thrown in %s on line %d

ext/standard/tests/misc/time_nanosleep_error5.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ time_nanosleep — Delay for a number of seconds and nanoseconds
77
--FILE--
88
<?php
99

10-
$nano = time_nanosleep(0, 1000000000);
10+
time_nanosleep(0, 1000000000);
1111

1212
?>
1313
--EXPECTF--
14-
Warning: time_nanosleep(): nanoseconds was not in the range 0 to 999 999 999 or seconds was negative in %s.php on line %d
14+
Fatal error: Uncaught ValueError: Nanoseconds was not in the range 0 to 999 999 999 or seconds was negative in %s:%d
15+
Stack trace:
16+
#0 %s(%d): time_nanosleep(0, 1000000000)
17+
#1 {main}
18+
thrown in %s on line %d

ext/standard/tests/time/bug60222.phpt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
Bug #60222 (time_nanosleep() does validate input params)
33
--FILE--
44
<?php
5-
var_dump(time_nanosleep(-1, 0));
6-
var_dump(time_nanosleep(0, -1));
5+
try {
6+
time_nanosleep(-1, 0);
7+
} catch (ValueError $exception) {
8+
echo $exception->getMessage() . "\n";
9+
}
10+
11+
try {
12+
time_nanosleep(0, -1);
13+
} catch (ValueError $exception) {
14+
echo $exception->getMessage() . "\n";
15+
}
716
?>
8-
===DONE===
917
--EXPECTF--
10-
Warning: time_nanosleep(): The seconds value must be greater than 0 in %s on line %d
11-
bool(false)
12-
13-
Warning: time_nanosleep(): The nanoseconds value must be greater than 0 in %s on line %d
14-
bool(false)
15-
===DONE===
18+
The seconds value must be greater than 0
19+
The nanoseconds value must be greater than 0

0 commit comments

Comments
 (0)