#36391 [Opn]: $a[] doesn't work correctly after array_pop

From: Date: Mon, 28 Jan 2008 14:42:14 +0000
Subject: #36391 [Opn]: $a[] doesn't work correctly after array_pop
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-121600@lists.php.net to get a copy of this message
 ID:               36391
 Updated by:       msaraujo@php.net
 Reported By:      tomas_matousek at hotmail dot com
 Status:           Open
 Bug Type:         Feature/Change Request
 Operating System: WinXP
 PHP Version:      5.1.2
 New Comment:

--- php-5.2.5/ext/standard/array.c	2007-11-06 11:28:21.000000000 -0200
+++ /usr/local/src/php-5.2.5/ext/standard/array.c	2008-01-28
06:35:34.000000000 -0200
@@ -1994,7 +1994,7 @@
 	     **val;			/* Value to be popped */
 	char *key = NULL;
 	int key_len = 0;
-	ulong index;
+	ulong index, new_index = 0;
 	
 	/* Get the arguments and do error-checking */
 	if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &stack) ==
FAILURE) {
@@ -2048,6 +2048,10 @@
 		}
 	} else if (!key_len && index >=
Z_ARRVAL_PP(stack)->nNextFreeElement-1) {
 		Z_ARRVAL_PP(stack)->nNextFreeElement =
Z_ARRVAL_PP(stack)->nNextFreeElement - 1;
+	}  else if (!key_len && index <
Z_ARRVAL_PP(stack)->nNextFreeElement-1) {
+      		    zend_hash_internal_pointer_end(Z_ARRVAL_PP(stack));
+                    zend_hash_get_current_key_ex(Z_ARRVAL_PP(stack),
&key, &key_len, &new_index, 0, NULL);
+                    Z_ARRVAL_PP(stack)->nNextFreeElement = new_index +
1;
 	}
 
 	zend_hash_internal_pointer_reset(Z_ARRVAL_PP(stack));


Previous Comments:
------------------------------------------------------------------------

[2006-02-14 17:33:38] tomas_matousek at hotmail dot com

Description:
------------
Operator $a[] doesn't add a correct key to the array if applied after
popping from array. It seems array_pop merely checks whether the removed
index is the max. int key remembered by the array and if yes it
decreases it by 1. I would expect array_pop to find the new maximal key
in the resulting array.

Reproduce code:
---------------
$a = array("a","b",100 => "c",200 => "d");
array_pop($a);
array_pop($a);
$a[] = "new";
var_dump($a);

Expected result:
----------------
array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(3) "new"
}




Actual result:
--------------
array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [199]=>
  string(3) "new"
}





------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=36391&edit=1


Thread (5 messages)

« previous php.bugs (#121600) next »