]> granicus.if.org Git - php/commitdiff
Fixed bug #40705 (Iterating within function moves original array pointer)
authorDmitry Stogov <dmitry@php.net>
Tue, 24 Jul 2007 19:24:56 +0000 (19:24 +0000)
committerDmitry Stogov <dmitry@php.net>
Tue, 24 Jul 2007 19:24:56 +0000 (19:24 +0000)
Fixed bug #40509 (key() function changed behaviour if global array is used within function)

Zend/tests/bug40509.phpt [new file with mode: 0755]
Zend/tests/bug40705.phpt [new file with mode: 0755]
Zend/zend_vm_def.h
Zend/zend_vm_execute.h

diff --git a/Zend/tests/bug40509.phpt b/Zend/tests/bug40509.phpt
new file mode 100755 (executable)
index 0000000..453060d
--- /dev/null
@@ -0,0 +1,26 @@
+--TEST--
+Bug #40509 key() function changed behaviour if global array is used within function 
+--FILE--
+<?php
+function foo()
+{
+       global $arr;
+       
+       $c = $arr["v"];
+       foreach ($c as $v) {}
+}
+
+$arr["v"] = array("a");
+
+var_dump(key($arr["v"]));
+foo();
+var_dump(key($arr["v"]));
+foreach ($arr["v"] as $k => $v) {
+       var_dump($k);
+}
+var_dump(key($arr["v"]));
+--EXPECT--
+int(0)
+int(0)
+int(0)
+NULL
diff --git a/Zend/tests/bug40705.phpt b/Zend/tests/bug40705.phpt
new file mode 100755 (executable)
index 0000000..198f985
--- /dev/null
@@ -0,0 +1,26 @@
+--TEST--
+Bug #40705 Iterating within function moves original array pointer 
+--FILE--
+<?php
+function doForeach($array)
+{
+    foreach ($array as $k => $v) {
+        // do stuff
+    }
+}
+
+$foo = array('foo', 'bar', 'baz');
+var_dump(key($foo));
+doForeach($foo);
+var_dump(key($foo));
+foreach ($foo as $k => $v) {
+       var_dump($k);
+}
+var_dump(key($foo));
+--EXPECT--
+int(0)
+int(0)
+int(0)
+int(1)
+int(2)
+NULL
index 479610cbcb8056a25b627cbb49e0dd60dc77e257..7fed3705c9de19a3142985d056fd516cb9d57276 100644 (file)
@@ -3324,11 +3324,9 @@ ZEND_VM_HANDLER(77, ZEND_FE_RESET, CONST|TMP|VAR|CV, ANY)
                                array_ptr->refcount++;
                        }
                } else {
-                       if (OP1_TYPE == IS_VAR &&
-                               free_op1.var == NULL &&
+                       if ((OP1_TYPE == IS_VAR || OP1_TYPE == IS_CV) &&
                            !array_ptr->is_ref &&
                            array_ptr->refcount > 1) {
-                               /* non-separated return value from function */
                                zval *tmp;
 
                                ALLOC_ZVAL(tmp);
index c985417621ee55d133d3638cb1916570da8d08df..c250f474d686d4edcb662b4d81853fe77be7eb18 100644 (file)
@@ -1969,7 +1969,7 @@ static int ZEND_UNSET_VAR_SPEC_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
 static int ZEND_FE_RESET_SPEC_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
 {
        zend_op *opline = EX(opline);
-       zend_free_op free_op1;
+
        zval *array_ptr, **array_ptr_ptr;
        HashTable *fe_ht;
        zend_object_iterator *iter = NULL;
@@ -2016,11 +2016,9 @@ static int ZEND_FE_RESET_SPEC_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
                                array_ptr->refcount++;
                        }
                } else {
-                       if (IS_CONST == IS_VAR &&
-                               free_op1.var == NULL &&
+                       if ((IS_CONST == IS_VAR || IS_CONST == IS_CV) &&
                            !array_ptr->is_ref &&
                            array_ptr->refcount > 1) {
-                               /* non-separated return value from function */
                                zval *tmp;
 
                                ALLOC_ZVAL(tmp);
@@ -5176,11 +5174,9 @@ static int ZEND_FE_RESET_SPEC_TMP_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
                                array_ptr->refcount++;
                        }
                } else {
-                       if (IS_TMP_VAR == IS_VAR &&
-                               free_op1.var == NULL &&
+                       if ((IS_TMP_VAR == IS_VAR || IS_TMP_VAR == IS_CV) &&
                            !array_ptr->is_ref &&
                            array_ptr->refcount > 1) {
-                               /* non-separated return value from function */
                                zval *tmp;
 
                                ALLOC_ZVAL(tmp);
@@ -8446,11 +8442,9 @@ static int ZEND_FE_RESET_SPEC_VAR_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
                                array_ptr->refcount++;
                        }
                } else {
-                       if (IS_VAR == IS_VAR &&
-                               free_op1.var == NULL &&
+                       if ((IS_VAR == IS_VAR || IS_VAR == IS_CV) &&
                            !array_ptr->is_ref &&
                            array_ptr->refcount > 1) {
-                               /* non-separated return value from function */
                                zval *tmp;
 
                                ALLOC_ZVAL(tmp);
@@ -21620,7 +21614,7 @@ static int ZEND_UNSET_VAR_SPEC_CV_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
 static int ZEND_FE_RESET_SPEC_CV_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
 {
        zend_op *opline = EX(opline);
-       zend_free_op free_op1;
+
        zval *array_ptr, **array_ptr_ptr;
        HashTable *fe_ht;
        zend_object_iterator *iter = NULL;
@@ -21667,11 +21661,9 @@ static int ZEND_FE_RESET_SPEC_CV_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
                                array_ptr->refcount++;
                        }
                } else {
-                       if (IS_CV == IS_VAR &&
-                               free_op1.var == NULL &&
+                       if ((IS_CV == IS_VAR || IS_CV == IS_CV) &&
                            !array_ptr->is_ref &&
                            array_ptr->refcount > 1) {
-                               /* non-separated return value from function */
                                zval *tmp;
 
                                ALLOC_ZVAL(tmp);