improve range array overflow error message (#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.
This commit is contained in:
divinity76 2024-12-30 18:53:16 +01:00 committed by GitHub
parent 34275564f2
commit 47e440019c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 13 additions and 6 deletions

View file

@ -2859,8 +2859,11 @@ PHP_FUNCTION(array_fill_keys)
#define RANGE_CHECK_DOUBLE_INIT_ARRAY(start, end, _step) do { \ #define RANGE_CHECK_DOUBLE_INIT_ARRAY(start, end, _step) do { \
double __calc_size = ((start - end) / (_step)) + 1; \ double __calc_size = ((start - end) / (_step)) + 1; \
if (__calc_size >= (double)HT_MAX_SIZE) { \ if (__calc_size >= (double)HT_MAX_SIZE) { \
double __exceed_by = __calc_size - (double)HT_MAX_SIZE; \
zend_value_error(\ 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(); \ RETURN_THROWS(); \
} \ } \
size = (uint32_t)_php_math_round(__calc_size, 0, PHP_ROUND_HALF_UP); \ size = (uint32_t)_php_math_round(__calc_size, 0, PHP_ROUND_HALF_UP); \
@ -2871,8 +2874,12 @@ PHP_FUNCTION(array_fill_keys)
#define RANGE_CHECK_LONG_INIT_ARRAY(start, end, _step) do { \ #define RANGE_CHECK_LONG_INIT_ARRAY(start, end, _step) do { \
zend_ulong __calc_size = ((zend_ulong) start - end) / (_step); \ zend_ulong __calc_size = ((zend_ulong) start - end) / (_step); \
if (__calc_size >= HT_MAX_SIZE - 1) { \ if (__calc_size >= HT_MAX_SIZE - 1) { \
uint64_t __excess = __calc_size - (HT_MAX_SIZE - 1); \
zend_value_error(\ 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(); \ RETURN_THROWS(); \
} \ } \
size = (uint32_t)(__calc_size + 1); \ size = (uint32_t)(__calc_size + 1); \

View file

@ -9,4 +9,4 @@ try {
} }
?> ?>
--EXPECTF-- --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.

View file

@ -9,4 +9,4 @@ try {
} }
?> ?>
--EXPECTF-- --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.

View file

@ -14,5 +14,5 @@ try {
} }
?> ?>
--EXPECTF-- --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 by %f elements: start=0.0, end=%f, step=0.1. Max size: %d
The supplied range exceeds the maximum array size: start=-%d end=%d step=1 The supplied range exceeds the maximum array size by %d elements: start=-%d, end=%d, step=1. Calculated size: %d. Maximum size: %d.