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 SplFixedArrayNoLongerAcceptsStrings for SPL related Bug #60341

Patch version 2011-12-12 21:08 UTC

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

Obsoleted by patches:

This patch renders other patches obsolete

Obsolete patches:

Patch Revisions:

Developer: morrison.levi@gmail.com

Index: ext/spl/tests/fixedarray_002.phpt
===================================================================
--- ext/spl/tests/fixedarray_002.phpt	(revision 319659)
+++ 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 (InvalidArgumentException $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 invalid
 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 319659)
+++ 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 319659)
+++ ext/spl/tests/fixedarray_004.phpt	(working copy)
@@ -14,5 +14,5 @@
 ?>
 ===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 319659)
+++ 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 319659)
+++ 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 319659)
+++ 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 319659)
+++ 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 319659)
+++ ext/spl/tests/fixedarray_001.phpt	(working copy)
@@ -6,17 +6,22 @@
 // errors
 try {
     $a[0] = "value1";
-} catch (RuntimeException $e) {
+} catch (OutOfBoundsException $e) {
     echo "Exception: ".$e->getMessage()."\n";
 }
 try {
+    $a['0'] = "value1"; //key as a string
+} catch (OutOfBoundsException $e) {
+    echo "Exception: ".$e->getMessage()."\n";
+}
+try {
     var_dump($a["asdf"]);
-} catch (RuntimeException $e) {
+} catch (InvalidArgumentException $e) {
     echo "Exception: ".$e->getMessage()."\n";
 }
 try {
     unset($a[-1]);
-} catch (RuntimeException $e) {
+} catch (OutOfBoundsException $e) {
     echo "Exception: ".$e->getMessage()."\n";
 }
 $a->setSize(10);
@@ -24,6 +29,7 @@
 
 $a[0] = "value0";
 $a[1] = "value1";
+$a['1'] = "value1";
 $a[2] = "value2";
 $a[3] = "value3";
 $ref = "value4";
@@ -46,9 +52,10 @@
 ?>
 ===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 invalid
+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 319659)
+++ ext/spl/spl_fixedarray.c	(working copy)
@@ -327,18 +327,26 @@
 	/* 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_STRING && is_numeric_string(offset->value.str.val, offset->value.str.len, &index, NULL, -1) != IS_LONG) {
+                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 +391,26 @@
 
 	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_RuntimeException, "Index invalid", 0 TSRMLS_CC);
+		return;
+	} else if(offsetType == IS_STRING && is_numeric_string(offset->value.str.val, offset->value.str.len, &index, NULL, -1) != IS_LONG) {
+                zend_throw_exception(spl_ce_InvalidArgumentException, "Index invalid", 0 TSRMLS_CC);
+		return NULL;
+	} else if (Z_TYPE_P(offset) != 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]) {
@@ -432,15 +448,23 @@
 static inline void spl_fixedarray_object_unset_dimension_helper(spl_fixedarray_object *intern, zval *offset TSRMLS_DC) /* {{{ */
 {
 	long index;
-	
-	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;
+	} else if(offsetType == IS_STRING && is_numeric_string(offset->value.str.val, offset->value.str.len, &index, NULL, -1) != IS_LONG) {
+                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;
 	} else {
 		if (intern->array->elements[index]) {
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 18:01:29 2024 UTC