Fixed bug #62904 (Crash when cloning an object which inherits SplFixedArray)
authorXinchen Hui <laruence@php.net>
Thu, 23 Aug 2012 15:27:16 +0000 (23:27 +0800)
committerXinchen Hui <laruence@php.net>
Thu, 23 Aug 2012 15:27:16 +0000 (23:27 +0800)
NEWS
ext/spl/spl_fixedarray.c
ext/spl/tests/bug62904.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 8492aa6c6b01fb8e0558996a85f692495984e87e..9af7977feed7c584301585cfa10ccfcf9f9d4694 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -39,6 +39,8 @@ PHP                                                                        NEWS
   . Fixed bug (segfault due to retval is not initialized). (Laruence)
 
 - SPL:
+  . Fixed bug #62904 (Crash when cloning an object which inherits SplFixedArray)
+    (Laruence)
   . Fixed bug #62616 (ArrayIterator::count() from IteratorIterator instance
     gives Segmentation fault). (Laruence, Gustavo)
 
index ee8f51eb33f2a98efa61955d205f336321fea8e6..0aac6d3f30e21b9259a9a369894fde8e3304a7bc 100644 (file)
@@ -223,10 +223,14 @@ static zend_object_value spl_fixedarray_object_new_ex(zend_class_entry *class_ty
        if (orig && clone_orig) {
                spl_fixedarray_object *other = (spl_fixedarray_object*)zend_object_store_get_object(orig TSRMLS_CC);
                intern->ce_get_iterator = other->ce_get_iterator;
-
-               intern->array = emalloc(sizeof(spl_fixedarray));
-               spl_fixedarray_init(intern->array, other->array->size TSRMLS_CC);
-               spl_fixedarray_copy(intern->array, other->array TSRMLS_CC);
+               if (!other->array) {
+                       /* leave a empty object, will be dtor later by CLONE handler */
+                       zend_throw_exception(spl_ce_RuntimeException, "The instance wasn't initialized properly", 0 TSRMLS_CC);
+               } else {
+                       intern->array = emalloc(sizeof(spl_fixedarray));
+                       spl_fixedarray_init(intern->array, other->array->size TSRMLS_CC);
+                       spl_fixedarray_copy(intern->array, other->array TSRMLS_CC);
+               }
        }
 
        while (parent) {
diff --git a/ext/spl/tests/bug62904.phpt b/ext/spl/tests/bug62904.phpt
new file mode 100644 (file)
index 0000000..7e392da
--- /dev/null
@@ -0,0 +1,19 @@
+--TEST--
+Bug #62904 (Crash when cloning an object which inherits SplFixedArray)
+--FILE--
+<?php
+
+class foo extends SplFixedArray {       
+    public function __construct($size) {
+    }
+}
+
+$x = new foo(2);
+
+try {
+    $z = clone $x;
+} catch (Exception $e) {
+    var_dump($e->getMessage());
+}
+--EXPECTF--
+string(40) "The instance wasn't initialized properly"