Skip to content

Commit aab3eff

Browse files
committed
Make empty strings an error
Analysis of OSS packages shows this never happens
1 parent d6982a6 commit aab3eff

File tree

3 files changed

+14
-43
lines changed

3 files changed

+14
-43
lines changed

ext/standard/array.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,14 +2688,8 @@ static uint8_t php_range_process_input(const zval *input, uint32_t arg_num, zend
26882688
return IS_DOUBLE;
26892689
case IS_STRING: {
26902690
if (Z_STRLEN_P(input) == 0) {
2691-
const char *arg_name = get_active_function_arg_name(arg_num);
2692-
php_error_docref(NULL, E_WARNING, "Argument #%d ($%s) must not be empty, casted to 0", arg_num, arg_name);
2693-
if (UNEXPECTED(EG(exception))) {
2694-
return 0;
2695-
}
2696-
*lval = 0;
2697-
*dval = 0.0;
2698-
return IS_LONG;
2691+
zend_argument_value_error(arg_num, "must not be empty");
2692+
return 0;
26992693
}
27002694
uint8_t type = is_numeric_str_function(Z_STR_P(input), lval, dval);
27012695
if (type == IS_DOUBLE) {

ext/standard/tests/array/range/bug32021.phpt

Lines changed: 0 additions & 17 deletions
This file was deleted.

ext/standard/tests/array/range/range_inputs_string_invalid.phpt

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,16 @@ echo "Range will ignore any byte after the first one\n";
99
var_dump( range("AA", "BB") );
1010

1111
echo "Range cannot operate on an empty string\n";
12-
var_dump( range("Z", "") ); // Both strings are cast to int, i.e. 0
13-
var_dump( range("", "Z") ); // Both strings are cast to int, i.e. 0
12+
try {
13+
var_dump( range("Z", "") );
14+
} catch (ValueError $e) {
15+
echo $e->getMessage(), "\n";
16+
}
17+
try {
18+
var_dump( range("", "Z") );
19+
} catch (ValueError $e) {
20+
echo $e->getMessage(), "\n";
21+
}
1422

1523
echo "Mixing numeric string and character\n";
1624
var_dump( range("1", "A") ); // The char is cast to an int, i.e. 0
@@ -36,22 +44,8 @@ array(2) {
3644
string(1) "B"
3745
}
3846
Range cannot operate on an empty string
39-
40-
Warning: range(): Argument #2 ($end) must not be empty, casted to 0 in %s on line %d
41-
42-
Warning: range(): Argument #2 ($end) must be a string if argument #1 ($start) is a string, argument #1 ($start) converted to 0 in %s on line %d
43-
array(1) {
44-
[0]=>
45-
int(0)
46-
}
47-
48-
Warning: range(): Argument #1 ($start) must not be empty, casted to 0 in %s on line %d
49-
50-
Warning: range(): Argument #1 ($start) must be a string if argument #2 ($end) is a string, argument #2 ($end) converted to 0 in %s on line %d
51-
array(1) {
52-
[0]=>
53-
int(0)
54-
}
47+
range(): Argument #2 ($end) must not be empty
48+
range(): Argument #1 ($start) must not be empty
5549
Mixing numeric string and character
5650

5751
Warning: range(): Argument #1 ($start) must be a string if argument #2 ($end) is a string, argument #2 ($end) converted to 0 in %s on line %d

0 commit comments

Comments
 (0)