php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48476 cloning extended DateTime class without calling parent::__constr crashed PHP
Submitted: 2009-06-05 09:29 UTC Modified: 2011-08-30 13:42 UTC
Votes:8
Avg. Score:3.8 ± 1.0
Reproduced:8 of 8 (100.0%)
Same Version:4 (50.0%)
Same OS:3 (37.5%)
From: rvanvelzen at expert-shops dot com Assigned: derick (profile)
Status: Closed Package: Reproducible crash
PHP Version: 5.2.9 OS: CentOS 5.2
Private report: No CVE-ID: None
 [2009-06-05 09:29 UTC] rvanvelzen at expert-shops dot com
Description:
------------
When trying to clone an extended class of DateTime of which the constructor doesn't call parent::__construct, this causes PHP to die.

Reproduce code:
---------------
<?php
class MyDateTime extends DateTime {
	public function __construct() { }
}

var_dump(clone new MyDateTime);
?>

Expected result:
----------------
object(MyDateTime)#2 (0) {
}


Actual result:
--------------
Nothing, PHP crashes

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-19 03:18 UTC] jasper at nerdsweide dot nl
This bug still exists in PHP 5.3.6:

-------------------------
class ExtendedDateTime extends DateTime
{}

$date1 = new ExtendedDateTime();
$date2 = clone $date1;           // <= ok!

-------------------------
class ExtendedDateTime extends DateTime
{
    public function __construct( $time = 'now', DateTimeZone $timezone = null )
    {
        parent::__construct( $time, $timezone );
    }
}

$date1 = new ExtendedDateTime();
$date2 = clone $date1;           // <= ok!

-------------------------
class ExtendedDateTime extends DateTime
{
    public function __construct( $time = 'now', DateTimeZone $timezone = null )
    {
    }
}

$date1 = new ExtendedDateTime();
$date2 = clone $date1;           // <= php crashes

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

In the apache2 error_log I find:
[notice] child pid 63203 exit signal Bus error (10)

Another note, when I implement a __clone method, it is called and run without problems:

-------------------------
class ExtendedDateTime extends DateTime
{
    public function __construct( $time = 'now', DateTimeZone $timezone = null )
    {
    }

    public function __clone()
    {
        var_dump( 'Am I run?' );
        exit;
    }
}

Outputs:

object(ExtendedDateTime)[345]
string 'Am I run?' (length=9)
-------------------------

What's even more unexpected is that the constructor has some effect on the language construct 'clone'. As far as I know the constructor isn't used when cloning an object.

The case I'm using is an extend of DateTime which wraps around an original DateTime. I've worded around this bug by implementing a 'copy' method:

-------------------------
class ExtendedDateTime extends DateTime
{
    protected $_datetime;

    public function __construct( $time = 'now', DateTimeZone $timezone = null )
    {
        $this->_datetime = new DateTime( $time, $timezone );
    }

    public function copy()
    {
        $copy = unserialize( 'O:9:"F500_Date":0:{}' );
        $copy->_datetime = clone $this->_datetime;
        return $copy;
    }
}

This works fine, but I'd like to be able to use the 'clone' language construct ;)


System:	Mac OS X 10.5.8 (9L31a)
Kernel:	Darwin 9.8.0
Apache: Apache/2.2.19 (Unix)
PHP:    5.3.6 (Zend Engine v2.3.0 with Xdebug v2.1.1)
 [2011-08-30 13:41 UTC] bjori@php.net
Automatic comment from SVN on behalf of bjori
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=315779
Log: Fixed bug#48476
 [2011-08-30 13:42 UTC] bjori@php.net
-Status: Assigned +Status: Closed
 [2011-08-30 13:42 UTC] bjori@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 09:01:30 2024 UTC