php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51938 serializing an object that implements serializable
Submitted: 2010-05-28 00:37 UTC Modified: 2010-05-28 01:00 UTC
From: phil dot brookes at gmail dot com Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.3.2 OS: CentOS kernel 2.6.18-164.11.1
Private report: No CVE-ID: None
 [2010-05-28 00:37 UTC] phil dot brookes at gmail dot com
Description:
------------
Hello,

Create a class and implement Serializable, then use the serialize function to return the value of serialize($this), apache crashes with an internal server error, the apache error log reports a Premature end of script headers error.

If line 17 is commented then the apache server does not crash.

Test script:
---------------
<?php
/**Simple class that is serializable and implements the serializable interface */
class ImplementsSerializable implements Serializable {
    /** returns the serialization of itself */
    public function serialize() {
        return serialize($this);
    }
    /** returns unserialized object passed in $serialized */
    public function unserialize($serialized) {
        return unserialize($serialized);
    }
}
/** Create a new ImplementSerializable object */
$serializable = new ImplementsSerializable();
/** Store the serialized value in $serialized, this causes apache to crash, with a
 *  Premature end of script headers error in the apache error log */
$serialized = $serializable->serialize();
/**point of execution does not reach here */
$unserialized = $serializable->unserialize($serialized);
echo "finished!";
?>


Expected result:
----------------
return value equal to the object serialized using the serialize function.

Actual result:
--------------
Apache crashes with internal server error and a Premature end of script headers error.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-28 01:00 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-05-28 01:00 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The serialize() method of an object implementing Serializable is called by serialize() so you get an recursion. you have to implement Serializable only when you want do use your own serialization logic for your class.

The serialize() method of the object shall not be called directly but by PHP's serializer.

serialize($serializable);

Your code gives infinite recursion. Infinite recursion is known to lead to a stack overflow.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 13:01:30 2024 UTC