From 7c6acc2eefcaf1b3fd03b1730db84f1f58fffec6 Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Tue, 20 Aug 2019 23:11:11 +0200 Subject: [PATCH] Promote warnings to errors in range() --- ext/standard/array.c | 20 +-- .../tests/array/range_bug70239_0.phpt | 10 +- .../tests/array/range_bug70239_1.phpt | 10 +- .../tests/array/range_bug70239_2.phpt | 9 +- .../tests/array/range_bug70239_3.phpt | 9 +- ext/standard/tests/array/range_errors.phpt | 119 +++++++++++------- 6 files changed, 111 insertions(+), 66 deletions(-) diff --git a/ext/standard/array.c b/ext/standard/array.c index 70523d479e..b99d3222dc 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -2704,8 +2704,9 @@ PHP_FUNCTION(array_fill_keys) #define RANGE_CHECK_DOUBLE_INIT_ARRAY(start, end) do { \ double __calc_size = ((start - end) / step) + 1; \ if (__calc_size >= (double)HT_MAX_SIZE) { \ - php_error_docref(NULL, E_WARNING, "The supplied range exceeds the maximum array size: start=%0.0f end=%0.0f", end, start); \ - RETURN_FALSE; \ + zend_throw_error(NULL, \ + "The supplied range exceeds the maximum array size: start=%0.0f end=%0.0f", end, start); \ + return; \ } \ size = (uint32_t)_php_math_round(__calc_size, 0, PHP_ROUND_HALF_UP); \ array_init_size(return_value, size); \ @@ -2715,15 +2716,16 @@ PHP_FUNCTION(array_fill_keys) #define RANGE_CHECK_LONG_INIT_ARRAY(start, end) do { \ zend_ulong __calc_size = ((zend_ulong) start - end) / lstep; \ if (__calc_size >= HT_MAX_SIZE - 1) { \ - php_error_docref(NULL, E_WARNING, "The supplied range exceeds the maximum array size: start=" ZEND_LONG_FMT " end=" ZEND_LONG_FMT, end, start); \ - RETURN_FALSE; \ + zend_throw_error(NULL, \ + "The supplied range exceeds the maximum array size: start=" ZEND_LONG_FMT " end=" ZEND_LONG_FMT, end, start); \ + return; \ } \ size = (uint32_t)(__calc_size + 1); \ array_init_size(return_value, size); \ zend_hash_real_init_packed(Z_ARRVAL_P(return_value)); \ } while (0) -/* {{{ proto array|false range(mixed low, mixed high[, int step]) +/* {{{ proto array range(mixed low, mixed high[, int step]) Create an array containing the range of integers or characters from low to high (inclusive) */ PHP_FUNCTION(range) { @@ -2812,8 +2814,8 @@ double_str: high = zval_get_double(zhigh); if (zend_isinf(high) || zend_isinf(low)) { - php_error_docref(NULL, E_WARNING, "Invalid range supplied: start=%0.0f end=%0.0f", low, high); - RETURN_FALSE; + zend_throw_error(NULL, "Invalid range supplied: start=%0.0f end=%0.0f", low, high); + return; } if (low > high) { /* Negative steps */ @@ -2905,8 +2907,8 @@ long_str: } err: if (err) { - php_error_docref(NULL, E_WARNING, "step exceeds the specified range"); - RETURN_FALSE; + zend_throw_error(NULL, "step exceeds the specified range"); + return; } } /* }}} */ diff --git a/ext/standard/tests/array/range_bug70239_0.phpt b/ext/standard/tests/array/range_bug70239_0.phpt index edfdd05fbc..c902a168b2 100644 --- a/ext/standard/tests/array/range_bug70239_0.phpt +++ b/ext/standard/tests/array/range_bug70239_0.phpt @@ -2,9 +2,13 @@ Bug #70239 Creating a huge array doesn't result in exhausted, but segfault, var 1 --FILE-- getMessage() . "\n"; +} ?> ===DONE=== ---EXPECTF-- -Warning: range(): Invalid range supplied: start=0 end=inf in %srange_bug70239_0.php on line %d +--EXPECT-- +Invalid range supplied: start=0 end=inf ===DONE=== diff --git a/ext/standard/tests/array/range_bug70239_1.phpt b/ext/standard/tests/array/range_bug70239_1.phpt index 75419cf829..6d12348f67 100644 --- a/ext/standard/tests/array/range_bug70239_1.phpt +++ b/ext/standard/tests/array/range_bug70239_1.phpt @@ -2,9 +2,13 @@ Bug #70239 Creating a huge array doesn't result in exhausted, but segfault, var 2 --FILE-- getMessage() . "\n"; +} ?> ===DONE=== ---EXPECTF-- -Warning: range(): Invalid range supplied: start=inf end=inf in %srange_bug70239_1.php on line %d +--EXPECT-- +Invalid range supplied: start=inf end=inf ===DONE=== diff --git a/ext/standard/tests/array/range_bug70239_2.phpt b/ext/standard/tests/array/range_bug70239_2.phpt index 76ed638669..c9396b3cb4 100644 --- a/ext/standard/tests/array/range_bug70239_2.phpt +++ b/ext/standard/tests/array/range_bug70239_2.phpt @@ -2,10 +2,13 @@ Bug #70239 Creating a huge array doesn't result in exhausted, but segfault, var 3 --FILE-- getMessage() . "\n"; +} ?> ===DONE=== --EXPECTF-- -Warning: range(): The supplied range exceeds the maximum array size: start=0 end=%d in %srange_bug70239_2.php on line %d -bool(false) +The supplied range exceeds the maximum array size: start=0 end=%d ===DONE=== diff --git a/ext/standard/tests/array/range_bug70239_3.phpt b/ext/standard/tests/array/range_bug70239_3.phpt index 8402870c82..e49267bfe3 100644 --- a/ext/standard/tests/array/range_bug70239_3.phpt +++ b/ext/standard/tests/array/range_bug70239_3.phpt @@ -2,10 +2,13 @@ Bug #70239 Creating a huge array doesn't result in exhausted, but segfault, var 4 --FILE-- getMessage() . "\n"; +} ?> ===DONE=== --EXPECTF-- -Warning: range(): The supplied range exceeds the maximum array size: start=-%d end=0 in %srange_bug70239_3.php on line %d -bool(false) +The supplied range exceeds the maximum array size: start=-%d end=0 ===DONE=== diff --git a/ext/standard/tests/array/range_errors.phpt b/ext/standard/tests/array/range_errors.phpt index bd29794502..971eade4be 100644 --- a/ext/standard/tests/array/range_errors.phpt +++ b/ext/standard/tests/array/range_errors.phpt @@ -7,37 +7,82 @@ precision=14 echo "\n*** Testing error conditions ***\n"; -echo "\n-- Testing ( (low < high) && (step = 0) ) --"; -var_dump( range(1, 2, 0) ); -var_dump( range("a", "b", 0) ); +echo "\n-- Testing ( (low < high) && (step = 0) ) --\n"; +try { + var_dump( range(1, 2, 0) ); +} catch (\Error $e) { + echo $e->getMessage(), "\n"; +} -echo "\n\n-- Testing ( (low > high) && (step = 0) ) --"; -var_dump( range(2, 1, 0) ); -var_dump( range("b", "a", 0) ); +try { + var_dump( range("a", "b", 0) ); +} catch (\Error $e) { + echo $e->getMessage(), "\n"; +} -echo "\n\n-- Testing ( (low < high) && (high-low < step) ) --"; -var_dump( range(1.0, 7.0, 6.5) ); +echo "\n\n-- Testing ( (low > high) && (step = 0) ) --\n"; +try { + var_dump( range(2, 1, 0) ); +} catch (\Error $e) { + echo $e->getMessage(), "\n"; +} -echo "\n\n-- Testing ( (low > high) && (low-high < step) ) --"; -var_dump( range(7.0, 1.0, 6.5) ); +try { + var_dump( range("b", "a", 0) ); +} catch (\Error $e) { + echo $e->getMessage(), "\n"; +} + +echo "\n\n-- Testing ( (low < high) && (high-low < step) ) --\n"; +try { + var_dump( range(1.0, 7.0, 6.5) ); +} catch (\Error $e) { + echo $e->getMessage(), "\n"; +} + +echo "\n\n-- Testing ( (low > high) && (low-high < step) ) --\n"; +try { + var_dump( range(7.0, 1.0, 6.5) ); +} catch (\Error $e) { + echo $e->getMessage(), "\n"; +} + +echo "\n-- Testing other conditions --\n"; +try { + var_dump( range(-1, -2, 2) ); +} catch (\Error $e) { + echo $e->getMessage(), "\n"; +} -echo "\n-- Testing other conditions --"; -var_dump( range(-1, -2, 2) ); try { var_dump( range("a", "j", "z") ); } catch (TypeError $e) { echo $e->getMessage(), "\n"; +} catch (\Error $e) { + echo $e->getMessage(), "\n"; +} + +try { + var_dump( range(0, 1, "140962482048819216326.24") ); +} catch (\Error $e) { + echo $e->getMessage(), "\n"; +} + +try { + var_dump( range(0, 1, "140962482048819216326.24.") ); +} catch (\Error $e) { + echo $e->getMessage(), "\n"; } -var_dump( range(0, 1, "140962482048819216326.24") ); -var_dump( range(0, 1, "140962482048819216326.24.") ); -echo "\n-- Testing Invalid steps --"; +echo "\n-- Testing Invalid steps --\n"; $step_arr = array( "string", NULL, FALSE, "", "\0" ); foreach( $step_arr as $step ) { try { var_dump( range( 1, 5, $step ) ); - } catch (TypeError $e) { + } catch (\TypeError $e) { + echo $e->getMessage(), "\n"; + } catch (\Error $e) { echo $e->getMessage(), "\n"; } } @@ -48,50 +93,34 @@ echo "Done\n"; *** Testing error conditions *** -- Testing ( (low < high) && (step = 0) ) -- -Warning: range(): step exceeds the specified range in %s on line %d -bool(false) - -Warning: range(): step exceeds the specified range in %s on line %d -bool(false) +step exceeds the specified range +step exceeds the specified range -- Testing ( (low > high) && (step = 0) ) -- -Warning: range(): step exceeds the specified range in %s on line %d -bool(false) - -Warning: range(): step exceeds the specified range in %s on line %d -bool(false) +step exceeds the specified range +step exceeds the specified range -- Testing ( (low < high) && (high-low < step) ) -- -Warning: range(): step exceeds the specified range in %s on line %d -bool(false) +step exceeds the specified range -- Testing ( (low > high) && (low-high < step) ) -- -Warning: range(): step exceeds the specified range in %s on line %d -bool(false) +step exceeds the specified range -- Testing other conditions -- -Warning: range(): step exceeds the specified range in %s on line %d -bool(false) +step exceeds the specified range range() expects parameter 3 to be int or float, string given - -Warning: range(): step exceeds the specified range in %s on line %d -bool(false) +step exceeds the specified range Notice: A non well formed numeric value encountered in %s on line %d +step exceeds the specified range -Warning: range(): step exceeds the specified range in %s on line %d -bool(false) - --- Testing Invalid steps --range() expects parameter 3 to be int or float, string given - -Warning: range(): step exceeds the specified range in %s on line %d -bool(false) - -Warning: range(): step exceeds the specified range in %s on line %d -bool(false) +-- Testing Invalid steps -- +range() expects parameter 3 to be int or float, string given +step exceeds the specified range +step exceeds the specified range range() expects parameter 3 to be int or float, string given range() expects parameter 3 to be int or float, string given Done -- 2.40.0