Skip to content

Commit 7c6acc2

Browse files
committed
Promote warnings to errors in range()
1 parent 5b8e12a commit 7c6acc2

File tree

6 files changed

+111
-66
lines changed

6 files changed

+111
-66
lines changed

ext/standard/array.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,8 +2704,9 @@ PHP_FUNCTION(array_fill_keys)
27042704
#define RANGE_CHECK_DOUBLE_INIT_ARRAY(start, end) do { \
27052705
double __calc_size = ((start - end) / step) + 1; \
27062706
if (__calc_size >= (double)HT_MAX_SIZE) { \
2707-
php_error_docref(NULL, E_WARNING, "The supplied range exceeds the maximum array size: start=%0.0f end=%0.0f", end, start); \
2708-
RETURN_FALSE; \
2707+
zend_throw_error(NULL, \
2708+
"The supplied range exceeds the maximum array size: start=%0.0f end=%0.0f", end, start); \
2709+
return; \
27092710
} \
27102711
size = (uint32_t)_php_math_round(__calc_size, 0, PHP_ROUND_HALF_UP); \
27112712
array_init_size(return_value, size); \
@@ -2715,15 +2716,16 @@ PHP_FUNCTION(array_fill_keys)
27152716
#define RANGE_CHECK_LONG_INIT_ARRAY(start, end) do { \
27162717
zend_ulong __calc_size = ((zend_ulong) start - end) / lstep; \
27172718
if (__calc_size >= HT_MAX_SIZE - 1) { \
2718-
php_error_docref(NULL, E_WARNING, "The supplied range exceeds the maximum array size: start=" ZEND_LONG_FMT " end=" ZEND_LONG_FMT, end, start); \
2719-
RETURN_FALSE; \
2719+
zend_throw_error(NULL, \
2720+
"The supplied range exceeds the maximum array size: start=" ZEND_LONG_FMT " end=" ZEND_LONG_FMT, end, start); \
2721+
return; \
27202722
} \
27212723
size = (uint32_t)(__calc_size + 1); \
27222724
array_init_size(return_value, size); \
27232725
zend_hash_real_init_packed(Z_ARRVAL_P(return_value)); \
27242726
} while (0)
27252727

2726-
/* {{{ proto array|false range(mixed low, mixed high[, int step])
2728+
/* {{{ proto array range(mixed low, mixed high[, int step])
27272729
Create an array containing the range of integers or characters from low to high (inclusive) */
27282730
PHP_FUNCTION(range)
27292731
{
@@ -2812,8 +2814,8 @@ PHP_FUNCTION(range)
28122814
high = zval_get_double(zhigh);
28132815

28142816
if (zend_isinf(high) || zend_isinf(low)) {
2815-
php_error_docref(NULL, E_WARNING, "Invalid range supplied: start=%0.0f end=%0.0f", low, high);
2816-
RETURN_FALSE;
2817+
zend_throw_error(NULL, "Invalid range supplied: start=%0.0f end=%0.0f", low, high);
2818+
return;
28172819
}
28182820

28192821
if (low > high) { /* Negative steps */
@@ -2905,8 +2907,8 @@ PHP_FUNCTION(range)
29052907
}
29062908
err:
29072909
if (err) {
2908-
php_error_docref(NULL, E_WARNING, "step exceeds the specified range");
2909-
RETURN_FALSE;
2910+
zend_throw_error(NULL, "step exceeds the specified range");
2911+
return;
29102912
}
29112913
}
29122914
/* }}} */

ext/standard/tests/array/range_bug70239_0.phpt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
Bug #70239 Creating a huge array doesn't result in exhausted, but segfault, var 1
33
--FILE--
44
<?php
5-
range(0, pow(2.0, 100000000));
5+
try {
6+
range(0, pow(2.0, 100000000));
7+
} catch (\Error $e) {
8+
echo $e->getMessage() . "\n";
9+
}
610
?>
711
===DONE===
8-
--EXPECTF--
9-
Warning: range(): Invalid range supplied: start=0 end=inf in %srange_bug70239_0.php on line %d
12+
--EXPECT--
13+
Invalid range supplied: start=0 end=inf
1014
===DONE===

ext/standard/tests/array/range_bug70239_1.phpt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
Bug #70239 Creating a huge array doesn't result in exhausted, but segfault, var 2
33
--FILE--
44
<?php
5-
range(pow(2.0, 100000000), pow(2.0, 100000000) + 1);
5+
try {
6+
range(pow(2.0, 100000000), pow(2.0, 100000000) + 1);
7+
} catch (\Error $e) {
8+
echo $e->getMessage() . "\n";
9+
}
610
?>
711
===DONE===
8-
--EXPECTF--
9-
Warning: range(): Invalid range supplied: start=inf end=inf in %srange_bug70239_1.php on line %d
12+
--EXPECT--
13+
Invalid range supplied: start=inf end=inf
1014
===DONE===

ext/standard/tests/array/range_bug70239_2.phpt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
Bug #70239 Creating a huge array doesn't result in exhausted, but segfault, var 3
33
--FILE--
44
<?php
5-
var_dump(range(0, PHP_INT_MAX));
5+
try {
6+
var_dump(range(0, PHP_INT_MAX));
7+
} catch (\Error $e) {
8+
echo $e->getMessage() . "\n";
9+
}
610
?>
711
===DONE===
812
--EXPECTF--
9-
Warning: range(): The supplied range exceeds the maximum array size: start=0 end=%d in %srange_bug70239_2.php on line %d
10-
bool(false)
13+
The supplied range exceeds the maximum array size: start=0 end=%d
1114
===DONE===

ext/standard/tests/array/range_bug70239_3.phpt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
Bug #70239 Creating a huge array doesn't result in exhausted, but segfault, var 4
33
--FILE--
44
<?php
5-
var_dump(range(PHP_INT_MIN, 0));
5+
try {
6+
var_dump(range(PHP_INT_MIN, 0));
7+
} catch (\Error $e) {
8+
echo $e->getMessage() . "\n";
9+
}
610
?>
711
===DONE===
812
--EXPECTF--
9-
Warning: range(): The supplied range exceeds the maximum array size: start=-%d end=0 in %srange_bug70239_3.php on line %d
10-
bool(false)
13+
The supplied range exceeds the maximum array size: start=-%d end=0
1114
===DONE===

ext/standard/tests/array/range_errors.phpt

Lines changed: 74 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,82 @@ precision=14
77

88
echo "\n*** Testing error conditions ***\n";
99

10-
echo "\n-- Testing ( (low < high) && (step = 0) ) --";
11-
var_dump( range(1, 2, 0) );
12-
var_dump( range("a", "b", 0) );
10+
echo "\n-- Testing ( (low < high) && (step = 0) ) --\n";
11+
try {
12+
var_dump( range(1, 2, 0) );
13+
} catch (\Error $e) {
14+
echo $e->getMessage(), "\n";
15+
}
1316

14-
echo "\n\n-- Testing ( (low > high) && (step = 0) ) --";
15-
var_dump( range(2, 1, 0) );
16-
var_dump( range("b", "a", 0) );
17+
try {
18+
var_dump( range("a", "b", 0) );
19+
} catch (\Error $e) {
20+
echo $e->getMessage(), "\n";
21+
}
1722

18-
echo "\n\n-- Testing ( (low < high) && (high-low < step) ) --";
19-
var_dump( range(1.0, 7.0, 6.5) );
23+
echo "\n\n-- Testing ( (low > high) && (step = 0) ) --\n";
24+
try {
25+
var_dump( range(2, 1, 0) );
26+
} catch (\Error $e) {
27+
echo $e->getMessage(), "\n";
28+
}
2029

21-
echo "\n\n-- Testing ( (low > high) && (low-high < step) ) --";
22-
var_dump( range(7.0, 1.0, 6.5) );
30+
try {
31+
var_dump( range("b", "a", 0) );
32+
} catch (\Error $e) {
33+
echo $e->getMessage(), "\n";
34+
}
35+
36+
echo "\n\n-- Testing ( (low < high) && (high-low < step) ) --\n";
37+
try {
38+
var_dump( range(1.0, 7.0, 6.5) );
39+
} catch (\Error $e) {
40+
echo $e->getMessage(), "\n";
41+
}
42+
43+
echo "\n\n-- Testing ( (low > high) && (low-high < step) ) --\n";
44+
try {
45+
var_dump( range(7.0, 1.0, 6.5) );
46+
} catch (\Error $e) {
47+
echo $e->getMessage(), "\n";
48+
}
49+
50+
echo "\n-- Testing other conditions --\n";
51+
try {
52+
var_dump( range(-1, -2, 2) );
53+
} catch (\Error $e) {
54+
echo $e->getMessage(), "\n";
55+
}
2356

24-
echo "\n-- Testing other conditions --";
25-
var_dump( range(-1, -2, 2) );
2657
try {
2758
var_dump( range("a", "j", "z") );
2859
} catch (TypeError $e) {
2960
echo $e->getMessage(), "\n";
61+
} catch (\Error $e) {
62+
echo $e->getMessage(), "\n";
63+
}
64+
65+
try {
66+
var_dump( range(0, 1, "140962482048819216326.24") );
67+
} catch (\Error $e) {
68+
echo $e->getMessage(), "\n";
69+
}
70+
71+
try {
72+
var_dump( range(0, 1, "140962482048819216326.24.") );
73+
} catch (\Error $e) {
74+
echo $e->getMessage(), "\n";
3075
}
31-
var_dump( range(0, 1, "140962482048819216326.24") );
32-
var_dump( range(0, 1, "140962482048819216326.24.") );
3376

34-
echo "\n-- Testing Invalid steps --";
77+
echo "\n-- Testing Invalid steps --\n";
3578
$step_arr = array( "string", NULL, FALSE, "", "\0" );
3679

3780
foreach( $step_arr as $step ) {
3881
try {
3982
var_dump( range( 1, 5, $step ) );
40-
} catch (TypeError $e) {
83+
} catch (\TypeError $e) {
84+
echo $e->getMessage(), "\n";
85+
} catch (\Error $e) {
4186
echo $e->getMessage(), "\n";
4287
}
4388
}
@@ -48,50 +93,34 @@ echo "Done\n";
4893
*** Testing error conditions ***
4994

5095
-- Testing ( (low < high) && (step = 0) ) --
51-
Warning: range(): step exceeds the specified range in %s on line %d
52-
bool(false)
53-
54-
Warning: range(): step exceeds the specified range in %s on line %d
55-
bool(false)
96+
step exceeds the specified range
97+
step exceeds the specified range
5698

5799

58100
-- Testing ( (low > high) && (step = 0) ) --
59-
Warning: range(): step exceeds the specified range in %s on line %d
60-
bool(false)
61-
62-
Warning: range(): step exceeds the specified range in %s on line %d
63-
bool(false)
101+
step exceeds the specified range
102+
step exceeds the specified range
64103

65104

66105
-- Testing ( (low < high) && (high-low < step) ) --
67-
Warning: range(): step exceeds the specified range in %s on line %d
68-
bool(false)
106+
step exceeds the specified range
69107

70108

71109
-- Testing ( (low > high) && (low-high < step) ) --
72-
Warning: range(): step exceeds the specified range in %s on line %d
73-
bool(false)
110+
step exceeds the specified range
74111

75112
-- Testing other conditions --
76-
Warning: range(): step exceeds the specified range in %s on line %d
77-
bool(false)
113+
step exceeds the specified range
78114
range() expects parameter 3 to be int or float, string given
79-
80-
Warning: range(): step exceeds the specified range in %s on line %d
81-
bool(false)
115+
step exceeds the specified range
82116

83117
Notice: A non well formed numeric value encountered in %s on line %d
118+
step exceeds the specified range
84119

85-
Warning: range(): step exceeds the specified range in %s on line %d
86-
bool(false)
87-
88-
-- Testing Invalid steps --range() expects parameter 3 to be int or float, string given
89-
90-
Warning: range(): step exceeds the specified range in %s on line %d
91-
bool(false)
92-
93-
Warning: range(): step exceeds the specified range in %s on line %d
94-
bool(false)
120+
-- Testing Invalid steps --
121+
range() expects parameter 3 to be int or float, string given
122+
step exceeds the specified range
123+
step exceeds the specified range
95124
range() expects parameter 3 to be int or float, string given
96125
range() expects parameter 3 to be int or float, string given
97126
Done

0 commit comments

Comments
 (0)