Skip to content

Commit 47e4400

Browse files
authored
improve range array overflow error message (php#16510)
Improve range array overflow error message Added info about "how much it exceeded" and the maximum allowable array size. Makes debugging easier when encountering this specific issue.
1 parent 3427556 commit 47e4400

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

ext/standard/array.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -2859,8 +2859,11 @@ PHP_FUNCTION(array_fill_keys)
28592859
#define RANGE_CHECK_DOUBLE_INIT_ARRAY(start, end, _step) do { \
28602860
double __calc_size = ((start - end) / (_step)) + 1; \
28612861
if (__calc_size >= (double)HT_MAX_SIZE) { \
2862+
double __exceed_by = __calc_size - (double)HT_MAX_SIZE; \
28622863
zend_value_error(\
2863-
"The supplied range exceeds the maximum array size: start=%0.1f end=%0.1f step=%0.1f", end, start, (_step)); \
2864+
"The supplied range exceeds the maximum array size by %.1f elements: " \
2865+
"start=%.1f, end=%.1f, step=%.1f. Max size: %.0f", \
2866+
__exceed_by, end, start, (_step), (double)HT_MAX_SIZE); \
28642867
RETURN_THROWS(); \
28652868
} \
28662869
size = (uint32_t)_php_math_round(__calc_size, 0, PHP_ROUND_HALF_UP); \
@@ -2871,8 +2874,12 @@ PHP_FUNCTION(array_fill_keys)
28712874
#define RANGE_CHECK_LONG_INIT_ARRAY(start, end, _step) do { \
28722875
zend_ulong __calc_size = ((zend_ulong) start - end) / (_step); \
28732876
if (__calc_size >= HT_MAX_SIZE - 1) { \
2877+
uint64_t __excess = __calc_size - (HT_MAX_SIZE - 1); \
28742878
zend_value_error(\
2875-
"The supplied range exceeds the maximum array size: start=" ZEND_LONG_FMT " end=" ZEND_LONG_FMT " step=" ZEND_LONG_FMT, end, start, (_step)); \
2879+
"The supplied range exceeds the maximum array size by %" PRIu64 " elements: " \
2880+
"start=" ZEND_LONG_FMT ", end=" ZEND_LONG_FMT ", step=" ZEND_LONG_FMT ". " \
2881+
"Calculated size: %" PRIu64 ". Maximum size: %" PRIu64 ".", \
2882+
__excess, end, start, (_step), (uint64_t)__calc_size, (uint64_t)HT_MAX_SIZE); \
28762883
RETURN_THROWS(); \
28772884
} \
28782885
size = (uint32_t)(__calc_size + 1); \

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ try {
99
}
1010
?>
1111
--EXPECTF--
12-
The supplied range exceeds the maximum array size: start=0 end=%d step=1
12+
The supplied range exceeds the maximum array size by %d elements: start=0, end=%d, step=1. Calculated size: %d. Maximum size: %d.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ try {
99
}
1010
?>
1111
--EXPECTF--
12-
The supplied range exceeds the maximum array size: start=-%d end=0 step=1
12+
The supplied range exceeds the maximum array size by %d elements: start=-%d, end=0, step=1. Calculated size: %d. Maximum size: %d.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ try {
1414
}
1515
?>
1616
--EXPECTF--
17-
The supplied range exceeds the maximum array size: start=0.0 end=100000000000.0 step=0.1
18-
The supplied range exceeds the maximum array size: start=-%d end=%d step=1
17+
The supplied range exceeds the maximum array size by %f elements: start=0.0, end=%f, step=0.1. Max size: %d
18+
The supplied range exceeds the maximum array size by %d elements: start=-%d, end=%d, step=1. Calculated size: %d. Maximum size: %d.

0 commit comments

Comments
 (0)