From d5e9ef8f0fad00618de62949d362d0980c6250f9 Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Wed, 21 Aug 2019 00:30:36 +0200 Subject: [PATCH] Promote warnings to error in array_flip() Closes GH-4576. --- ext/standard/array.c | 14 +++++++------- ext/standard/tests/array/array_fill_error.phpt | 15 ++++++++++----- ext/standard/tests/array/bug61058.phpt | 11 ++++++++--- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/ext/standard/array.c b/ext/standard/array.c index 96f9eb4893..7296e0f607 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -2606,7 +2606,7 @@ PHP_FUNCTION(compact) } /* }}} */ -/* {{{ proto array|false array_fill(int start_key, int num, mixed val) +/* {{{ proto array array_fill(int start_key, int num, mixed val) Create an array containing num elements starting with index start_key each initialized to val */ PHP_FUNCTION(array_fill) { @@ -2621,11 +2621,11 @@ PHP_FUNCTION(array_fill) if (EXPECTED(num > 0)) { if (sizeof(num) > 4 && UNEXPECTED(EXPECTED(num > 0x7fffffff))) { - php_error_docref(NULL, E_WARNING, "Too many elements"); - RETURN_FALSE; + zend_throw_error(NULL, "Too many elements"); + return; } else if (UNEXPECTED(start_key > ZEND_LONG_MAX - num + 1)) { - php_error_docref(NULL, E_WARNING, "Cannot add element to the array as the next element is already occupied"); - RETURN_FALSE; + zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied"); + return; } else if (EXPECTED(start_key >= 0) && EXPECTED(start_key < num)) { /* create packed array */ Bucket *p; @@ -2670,8 +2670,8 @@ PHP_FUNCTION(array_fill) } else if (EXPECTED(num == 0)) { RETURN_EMPTY_ARRAY(); } else { - php_error_docref(NULL, E_WARNING, "Number of elements can't be negative"); - RETURN_FALSE; + zend_throw_error(NULL, "Number of elements can't be negative"); + return; } } /* }}} */ diff --git a/ext/standard/tests/array/array_fill_error.phpt b/ext/standard/tests/array/array_fill_error.phpt index 386404a336..63af88bf28 100644 --- a/ext/standard/tests/array/array_fill_error.phpt +++ b/ext/standard/tests/array/array_fill_error.phpt @@ -14,13 +14,18 @@ echo "*** Testing array_fill() : error conditions ***\n"; $start_key = 0; $num = -1; $val = 1; -var_dump( array_fill($start_key,$num,$val) ); -echo "Done"; +try { + var_dump( array_fill($start_key,$num,$val) ); +} catch (Error $e) { + echo $e->getMessage() . "\n"; +} + ?> + +DONE --EXPECTF-- *** Testing array_fill() : error conditions *** +Number of elements can't be negative -Warning: array_fill(): Number of elements can't be negative in %s on line %d -bool(false) -Done +DONE diff --git a/ext/standard/tests/array/bug61058.phpt b/ext/standard/tests/array/bug61058.phpt index f5e06a4a02..6e78b35711 100644 --- a/ext/standard/tests/array/bug61058.phpt +++ b/ext/standard/tests/array/bug61058.phpt @@ -2,7 +2,12 @@ Bug #61058 (array_fill leaks if start index is PHP_INT_MAX) --FILE-- getMessage() . "\n"; +} ?> ---EXPECTF-- -Warning: array_fill(): Cannot add element to the array as the next element is already occupied in %sbug61058.php on line %d +--EXPECT-- +Cannot add element to the array as the next element is already occupied -- 2.40.0