]> granicus.if.org Git - php/commitdiff
Fixed bug #32674 (exception in iterator causes crash)
authorDmitry Stogov <dmitry@php.net>
Wed, 27 Apr 2005 06:46:44 +0000 (06:46 +0000)
committerDmitry Stogov <dmitry@php.net>
Wed, 27 Apr 2005 06:46:44 +0000 (06:46 +0000)
NEWS
Zend/tests/bug32674.phpt [new file with mode: 0644]
Zend/zend_execute.c

diff --git a/NEWS b/NEWS
index 86b5cb3b728eff198fccaad840006e1a6f7578d6..87da8c730b03c2065c07e21e02edc94ab3245a14 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -23,6 +23,7 @@ PHP                                                                        NEWS
   (Marcus)
 - Fixed bug #32682 (ext/mssql: Error on module shutdown when called from
   activescript). (Frank)
+- Fixed bug #32674 (exception in iterator causes crash). (Dmitry)
 - Fixed bug #32647 (Using register_shutdown_function() with invalid callback
   can crash PHP). (Jani)
 - Fixed bug #32615 (Segfault in replaceChild() using fragment when 
diff --git a/Zend/tests/bug32674.phpt b/Zend/tests/bug32674.phpt
new file mode 100644 (file)
index 0000000..547bcec
--- /dev/null
@@ -0,0 +1,62 @@
+--TEST--
+Bug #32674 exception in iterator causes crash
+--FILE--
+<?php
+class collection implements Iterator {
+
+  private $_elements = array();
+
+  public function __construct() {
+  }
+
+  public function rewind() {
+    reset($this->_elements);
+  }
+
+  public function count() {
+    return count($this->_elements);
+  }
+
+  public function current() {
+    $element = current($this->_elements);
+    return $element;
+  }
+
+  public function next() {
+    $element = next($this->_elements);
+    return $element;
+  }
+
+  public function key() {
+    $this->_fillCollection();
+    $element = key($this->_elements);
+    return $element;
+  }
+
+  public function valid() {
+    throw new Exception('shit happend');
+
+    return ($this->current() !== false);
+  }
+}
+
+class class2 {
+  public $dummy;
+}
+
+$obj = new class2();
+$col = new collection();
+
+try {
+       foreach($col as $co) {
+       //irrelevant
+       }
+       echo 'shouldn`t get here';
+       //$dummy = 'this will not crash'; 
+       $obj->dummy = 'this will crash';
+} catch (Exception $e) {
+       echo "ok\n";
+}
+?>
+--EXPECT--
+ok
index 3a26ae9d7120acb501ca3e9858fd2da290eef30f..a1512d8c2d67beaf6e14b176b3457b2fcee1ef54 100644 (file)
@@ -1333,7 +1333,7 @@ static int zend_check_symbol(zval **pz TSRMLS_DC)
 
 #define SET_OPCODE(new_op)     \
        CHECK_SYMBOL_TABLES()   \
-       EX(opline) = new_op;
+       EX(opline) = new_op; \
 
 #define INC_OPCODE()                   \
        if (!EG(exception)) {           \
@@ -3784,6 +3784,11 @@ int zend_fe_reset_handler(ZEND_OPCODE_HANDLER_ARGS)
                iter->index = 0;
                if (iter->funcs->rewind) {
                        iter->funcs->rewind(iter TSRMLS_CC);
+                       if (EG(exception)) {
+                               array_ptr->refcount--;
+                               zval_ptr_dtor(&array_ptr);
+                               NEXT_OPCODE();
+                       }
                }
        } else if ((fe_ht = HASH_OF(array_ptr)) != NULL) {
                /* probably redundant */
@@ -3860,13 +3865,28 @@ int zend_fe_fetch_handler(ZEND_OPCODE_HANDLER_ARGS)
                                /* This could cause an endless loop if index becomes zero again.
                                 * In case that ever happens we need an additional flag. */
                                iter->funcs->move_forward(iter TSRMLS_CC);
+                               if (EG(exception)) {
+                                       array->refcount--;
+                                       zval_ptr_dtor(&array);
+                                       NEXT_OPCODE();
+                               }
                        }
                        if (!iter || iter->funcs->valid(iter TSRMLS_CC) == FAILURE) {
                                /* reached end of iteration */
+                               if (EG(exception)) {
+                                       array->refcount--;
+                                       zval_ptr_dtor(&array);
+                                       NEXT_OPCODE();
+                               }
                                SET_OPCODE(op_array->opcodes+opline->op2.u.opline_num);
                                return 0; /* CHECK_ME */
                        }       
                        iter->funcs->get_current_data(iter, &value TSRMLS_CC);
+                       if (EG(exception)) {
+                               array->refcount--;
+                               zval_ptr_dtor(&array);
+                               NEXT_OPCODE();
+                       }
                        if (!value) {
                                /* failure in get_current_data */
                                SET_OPCODE(op_array->opcodes+opline->op2.u.opline_num);
@@ -3874,6 +3894,11 @@ int zend_fe_fetch_handler(ZEND_OPCODE_HANDLER_ARGS)
                        }
                        if (iter->funcs->get_current_key) {
                                key_type = iter->funcs->get_current_key(iter, &str_key, &str_key_len, &int_key TSRMLS_CC);
+                               if (EG(exception)) {
+                                       array->refcount--;
+                                       zval_ptr_dtor(&array);
+                                       NEXT_OPCODE();
+                               }
                        } else {
                                key_type = HASH_KEY_IS_LONG;
                                int_key = iter->index;