Skip to content

Commit

Permalink
improve range array overflow error message (#16510)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
divinity76 authored Dec 30, 2024
1 parent 3427556 commit 47e4400
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
11 changes: 9 additions & 2 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2859,8 +2859,11 @@ PHP_FUNCTION(array_fill_keys)
#define RANGE_CHECK_DOUBLE_INIT_ARRAY(start, end, _step) do { \
double __calc_size = ((start - end) / (_step)) + 1; \
if (__calc_size >= (double)HT_MAX_SIZE) { \
double __exceed_by = __calc_size - (double)HT_MAX_SIZE; \
zend_value_error(\
"The supplied range exceeds the maximum array size: start=%0.1f end=%0.1f step=%0.1f", end, start, (_step)); \
"The supplied range exceeds the maximum array size by %.1f elements: " \
"start=%.1f, end=%.1f, step=%.1f. Max size: %.0f", \
__exceed_by, end, start, (_step), (double)HT_MAX_SIZE); \
RETURN_THROWS(); \
} \
size = (uint32_t)_php_math_round(__calc_size, 0, PHP_ROUND_HALF_UP); \
Expand All @@ -2871,8 +2874,12 @@ PHP_FUNCTION(array_fill_keys)
#define RANGE_CHECK_LONG_INIT_ARRAY(start, end, _step) do { \
zend_ulong __calc_size = ((zend_ulong) start - end) / (_step); \
if (__calc_size >= HT_MAX_SIZE - 1) { \
uint64_t __excess = __calc_size - (HT_MAX_SIZE - 1); \
zend_value_error(\
"The supplied range exceeds the maximum array size: start=" ZEND_LONG_FMT " end=" ZEND_LONG_FMT " step=" ZEND_LONG_FMT, end, start, (_step)); \
"The supplied range exceeds the maximum array size by %" PRIu64 " elements: " \
"start=" ZEND_LONG_FMT ", end=" ZEND_LONG_FMT ", step=" ZEND_LONG_FMT ". " \
"Calculated size: %" PRIu64 ". Maximum size: %" PRIu64 ".", \
__excess, end, start, (_step), (uint64_t)__calc_size, (uint64_t)HT_MAX_SIZE); \
RETURN_THROWS(); \
} \
size = (uint32_t)(__calc_size + 1); \
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/tests/array/range/range_bug70239_2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ try {
}
?>
--EXPECTF--
The supplied range exceeds the maximum array size: start=0 end=%d step=1
The supplied range exceeds the maximum array size by %d elements: start=0, end=%d, step=1. Calculated size: %d. Maximum size: %d.
2 changes: 1 addition & 1 deletion ext/standard/tests/array/range/range_bug70239_3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ try {
}
?>
--EXPECTF--
The supplied range exceeds the maximum array size: start=-%d end=0 step=1
The supplied range exceeds the maximum array size by %d elements: start=-%d, end=0, step=1. Calculated size: %d. Maximum size: %d.
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ try {
}
?>
--EXPECTF--
The supplied range exceeds the maximum array size: start=0.0 end=100000000000.0 step=0.1
The supplied range exceeds the maximum array size: start=-%d end=%d step=1
The supplied range exceeds the maximum array size by %f elements: start=0.0, end=%f, step=0.1. Max size: %d
The supplied range exceeds the maximum array size by %d elements: start=-%d, end=%d, step=1. Calculated size: %d. Maximum size: %d.

0 comments on commit 47e4400

Please sign in to comment.