php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #60341
Patch SplFixedArrayOptimizations revision 2011-12-12 23:21 UTC by morrison dot levi at gmail dot com
Patch SplFixedArrayNoLongerAcceptsStrings revision 2011-12-12 21:08 UTC by morrison dot levi at gmail dot com
Patch SplFixedArrayThrowsSpecificExceptions revision 2011-11-22 21:54 UTC by morrison dot levi at gmail dot cm

Patch SplFixedArrayThrowsSpecificExceptions for SPL related Bug #60341

Patch version 2011-11-22 21:54 UTC

Return to Bug #60341 | Download this patch
This patch is obsolete

Obsoleted by patches:

Patch Revisions:

Developer: morrison.levi@gmail.cm

Index: ext/spl/tests/fixedarray_002.phpt
===================================================================
--- ext/spl/tests/fixedarray_002.phpt	(revision 319660)
+++ ext/spl/tests/fixedarray_002.phpt	(working copy)
@@ -30,17 +30,17 @@
 // errors
 try {
     $a[0] = "value1";
-} catch (RuntimeException $e) {
+} catch (OutOfBoundsException $e) {
     echo "Exception: ".$e->getMessage()."\n";
 }
 try {
     var_dump($a["asdf"]);
-} catch (RuntimeException $e) {
+} catch (OutOfBoundsException $e) {
     echo "Exception: ".$e->getMessage()."\n";
 }
 try {
     unset($a[-1]);
-} catch (RuntimeException $e) {
+} catch (OutOfBoundsException $e) {
     echo "Exception: ".$e->getMessage()."\n";
 }
 $a->setSize(10);
@@ -67,11 +67,11 @@
 ===DONE===
 --EXPECTF--
 A::offsetSet
-Exception: Index invalid or out of range
+Exception: Index out of range
 A::offsetGet
-Exception: Index invalid or out of range
+Exception: Index out of range
 A::offsetUnset
-Exception: Index invalid or out of range
+Exception: Index out of range
 A::offsetSet
 A::offsetSet
 A::offsetSet
Index: ext/spl/tests/fixedarray_012.phpt
===================================================================
--- ext/spl/tests/fixedarray_012.phpt	(revision 319660)
+++ ext/spl/tests/fixedarray_012.phpt	(working copy)
@@ -7,7 +7,7 @@
 
 try {
 	$b = &$a[];
-} catch (Exception $e) {
+} catch (InvalidArgumentException $e) {
 	echo $e->getMessage(), "\n";
 }
 
@@ -15,5 +15,5 @@
 
 ?>
 --EXPECT--
-Index invalid or out of range
+Index invalid: does not accept NULL
 ok
Index: ext/spl/tests/fixedarray_004.phpt
===================================================================
--- ext/spl/tests/fixedarray_004.phpt	(revision 319660)
+++ ext/spl/tests/fixedarray_004.phpt	(working copy)
@@ -7,12 +7,12 @@
 
 try {
 	$a[] = 1;
-} catch (Exception $e) {
+} catch (OverflowException $e) {
 	var_dump($e->getMessage());
 }
 
 ?>
 ===DONE===
 --EXPECTF--
-string(29) "Index invalid or out of range"
+string(31) "You cannot add to a fixed array"
 ===DONE===
Index: ext/spl/tests/fixedarray_013.phpt
===================================================================
--- ext/spl/tests/fixedarray_013.phpt	(revision 319660)
+++ ext/spl/tests/fixedarray_013.phpt	(working copy)
@@ -12,10 +12,10 @@
 
 try {
 	test($a[]);
-} catch (Exception $e) {
+} catch (InvalidArgumentException $e) {
 	echo $e->getMessage(), "\n";
 }
 
 ?>
 --EXPECT--
-Index invalid or out of range
+Index invalid: does not accept NULL
Index: ext/spl/tests/fixedarray_014.phpt
===================================================================
--- ext/spl/tests/fixedarray_014.phpt	(revision 319660)
+++ ext/spl/tests/fixedarray_014.phpt	(working copy)
@@ -6,10 +6,10 @@
 try {
 	$a = new SplFixedArray(NULL);
 	echo $a[0]++;
-} catch (Exception $e) {
+} catch (OutOfBoundsException $e) {
 	echo $e->getMessage();
 }
 
 ?>
 --EXPECT--
-Index invalid or out of range
+Index out of range
Index: ext/spl/tests/fixedarray_006.phpt
===================================================================
--- ext/spl/tests/fixedarray_006.phpt	(revision 319660)
+++ ext/spl/tests/fixedarray_006.phpt	(working copy)
@@ -10,7 +10,7 @@
 	for ($i = 0; $i < 100; $i++) {
 		$a[] = new stdClass;
 	}
-} catch (Exception $e) {
+} catch (OverflowException $e) {
 	echo $e->getMessage(), "\n";
 }
 
@@ -18,5 +18,5 @@
 
 ?>
 --EXPECT--
-Index invalid or out of range
+You cannot add to a fixed array
 ok
Index: ext/spl/tests/fixedarray_015.phpt
===================================================================
--- ext/spl/tests/fixedarray_015.phpt	(revision 319660)
+++ ext/spl/tests/fixedarray_015.phpt	(working copy)
@@ -7,17 +7,17 @@
 
 try {
 	var_dump($a[1]);
-} catch (Exception $e) {
+} catch (OutOfBoundsException $e) {
 	echo $e->getMessage(), "\n";
 }
 try {
 	$a[1] = 1;
-} catch (Exception $e) {
+} catch (OutOfBoundsException $e) {
 	echo $e->getMessage(), "\n";
 }
 try {
 	var_dump(count($a[1]));
-} catch (Exception $e) {
+} catch (OutOfBoundsException $e) {
 	echo $e->getMessage(), "\n";
 }
 try {
@@ -41,9 +41,9 @@
 ?>
 --EXPECTF--	
 Warning: SplFixedArray::__construct() expects parameter 1 to be long, string given in %s on line %d
-Index invalid or out of range
-Index invalid or out of range
-Index invalid or out of range
+Index out of range
+Index out of range
+Index out of range
 int(0)
 bool(true)
 Done
Index: ext/spl/tests/fixedarray_001.phpt
===================================================================
--- ext/spl/tests/fixedarray_001.phpt	(revision 319660)
+++ ext/spl/tests/fixedarray_001.phpt	(working copy)
@@ -6,17 +6,17 @@
 // errors
 try {
     $a[0] = "value1";
-} catch (RuntimeException $e) {
+} catch (OutOfBoundsException $e) {
     echo "Exception: ".$e->getMessage()."\n";
 }
 try {
     var_dump($a["asdf"]);
-} catch (RuntimeException $e) {
+} catch (OutOfBoundsException $e) {
     echo "Exception: ".$e->getMessage()."\n";
 }
 try {
     unset($a[-1]);
-} catch (RuntimeException $e) {
+} catch (OutOfBoundsException $e) {
     echo "Exception: ".$e->getMessage()."\n";
 }
 $a->setSize(10);
@@ -46,9 +46,9 @@
 ?>
 ===DONE===
 --EXPECTF--
-Exception: Index invalid or out of range
-Exception: Index invalid or out of range
-Exception: Index invalid or out of range
+Exception: Index out of range
+Exception: Index out of range
+Exception: Index out of range
 string(6) "value0"
 string(6) "value2"
 string(6) "value3"
Index: ext/spl/spl_fixedarray.c
===================================================================
--- ext/spl/spl_fixedarray.c	(revision 319660)
+++ ext/spl/spl_fixedarray.c	(working copy)
@@ -327,18 +327,23 @@
 	/* we have to return NULL on error here to avoid memleak because of 
 	 * ZE duplicating uninitialized_zval_ptr */
 	if (!offset) {
-		zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0 TSRMLS_CC);
+		zend_throw_exception(spl_ce_InvalidArgumentException, "Index invalid: does not accept NULL", 0 TSRMLS_CC);
 		return NULL;
 	}
 
-	if (Z_TYPE_P(offset) != IS_LONG) {
+	unsigned char offsetType = Z_TYPE_P(offset);
+	
+	if (offsetType == IS_OBJECT) {
+		zend_throw_exception(spl_ce_InvalidArgumentException, "Index invalid", 0 TSRMLS_CC);
+		return NULL;
+	} else if (offsetType != IS_LONG) {
 		index = spl_offset_convert_to_long(offset TSRMLS_CC);
 	} else {
 		index = Z_LVAL_P(offset);
 	}
 	
 	if (index < 0 || intern->array == NULL || index >= intern->array->size) {
-		zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0 TSRMLS_CC);
+		zend_throw_exception(spl_ce_OutOfBoundsException, "Index out of range", 0 TSRMLS_CC);
 		return NULL;
 	} else if(!intern->array->elements[index]) {
 		return NULL;
@@ -383,18 +388,23 @@
 
 	if (!offset) {
 		/* '$array[] = value' syntax is not supported */
-		zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0 TSRMLS_CC);
+		zend_throw_exception(spl_ce_OverflowException, "You cannot add to a fixed array", 0 TSRMLS_CC);
 		return;
 	}
 
-	if (Z_TYPE_P(offset) != IS_LONG) {
+	unsigned char offsetType = Z_TYPE_P(offset);
+
+	if (offsetType == IS_OBJECT) {
+		zend_throw_exception(spl_ce_InvalidArgumentException, "Invalid index", 0 TSRMLS_CC);
+		return;
+	} else if (offsetType != IS_LONG) {
 		index = spl_offset_convert_to_long(offset TSRMLS_CC);
 	} else {
 		index = Z_LVAL_P(offset);
 	}
 
 	if (index < 0 || intern->array == NULL || index >= intern->array->size) {
-		zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0 TSRMLS_CC);
+		zend_throw_exception(spl_ce_OutOfBoundsException, "Index out of range", 0 TSRMLS_CC);
 		return;
 	} else {
 		if (intern->array->elements[index]) {
@@ -440,7 +450,7 @@
 	}
 	
 	if (index < 0 || intern->array == NULL || index >= intern->array->size) {
-		zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0 TSRMLS_CC);
+		zend_throw_exception(spl_ce_OutOfBoundsException, "Index out of range", 0 TSRMLS_CC);
 		return;
 	} else {
 		if (intern->array->elements[index]) {
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 16:01:29 2024 UTC