Skip to content

Instantly share code, notes, and snippets.

@aurelijus
Created February 5, 2013 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aurelijus/4713758 to your computer and use it in GitHub Desktop.
Save aurelijus/4713758 to your computer and use it in GitHub Desktop.
Segmentation fault
<?php
class Permission implements \Serializable {
protected $id = 3;
public function serialize()
{
return serialize(array($this->id));
}
public function unserialize($serialized)
{
list($this->id) = unserialize($serialized);
}
}
class UserPermission implements \Serializable {
public $permission;
public $webshop;
public function serialize()
{
return serialize(array($this->webshop, $this->permission));
}
public function unserialize($serialized)
{
list($this->webshop, $this->permission) = unserialize($serialized);
}
}
class Webshop implements \Serializable {
protected $id = 13;
public function serialize()
{
return serialize(array($this->id));
}
public function unserialize($serialized)
{
list($this->id) = unserialize($serialized);
}
}
class AbstractToken implements \Serializable {
public $roles;
public function serialize()
{
return serialize(array($this->roles));
}
public function unserialize($serialized)
{
list($this->roles) = unserialize($serialized);
}
}
class UsernamePasswordToken extends AbstractToken {
private $credentials = null;
private $providerKey = null;
public function serialize()
{
return serialize(array($this->credentials, $this->providerKey, parent::serialize()));
}
public function unserialize($str)
{
list($this->credentials, $this->providerKey, $parentStr) = unserialize($str);
parent::unserialize($parentStr);
}
}
$token = new UsernamePasswordToken();
$webshop = new Webshop;
$permission = new Permission;
$roles = array();
for ($i = 0; $i < 2; $i++) {
$roles[$i] = new UserPermission();
$roles[$i]->webshop = $webshop;
$roles[$i]->permission = $permission;
}
$token->roles = $roles;
var_dump(unserialize(serialize($token)));
@aurelijus
Copy link
Author

Reproduced on

$ php -v

PHP 5.4.11 (cli) (built: Jan 16 2013 16:51:38) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
    with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans

$ cat /etc/redhat-release

Red Hat Enterprise Linux Server release 6.3 (Santiago)

$ php -v

PHP 5.4.11 (cli) (built: Jan 26 2013 00:11:35) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
    with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans

$ system_profiler SPSoftwareDataType

Software:
System Software Overview:
System Version: Mac OS X 10.7.5 (11G63b)
Kernel Version: Darwin 11.4.2

Could not reproduce on

$ php -v

PHP 5.4.0-ZS5.6.0 (cli) (built: Feb 20 2012 17:59:01) 
Copyright (coffee) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (coffee) 1998-2012 Zend Technologies
with Zend Extension Manager v5.1, Copyright (coffee) 2003-2010, by Zend Technologies
    with Zend Data Cache v4.0, Copyright (coffee) 2004-2010, by Zend Technologies [loaded] [licensed] [disabled]
    with Zend Job Queue v4.0, Copyright (coffee) 2004-2010, by Zend Technologies [loaded] [not licensed] [disabled]
    with Zend Utils v1.0, Copyright (coffee) 2004-2010, by Zend Technologies [loaded] [licensed] [enabled]
    with Zend Code Tracing v1.0, Copyright (coffee) 2009-2010, by Zend Technologies [loaded] [not licensed] [disabled]
    with Zend Page Cache v4.0, Copyright (coffee) 2004-2010, by Zend Technologies [loaded] [licensed] [disabled]

$ system_profiler SPSoftwareDataType

Software:
System Software Overview: 
System Version: OS X 10.8.2 (12C60)
Kernel Version: Darwin 12.2.0

@aurelijus
Copy link
Author

Cloning object in UserPermission::serialize helps:

public function serialize()
{
    $this->webshop = clone $this->webshop;
    $this->permission = clone $this->permission;
    return serialize(array($this->webshop, $this->permission));
}

@m6w6
Copy link

m6w6 commented Feb 6, 2013

Avoiding parent::(un)serialize() too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment