Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Feb 5, 2013

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

This comment has been minimized.

Copy link
Owner Author

commented Feb 5, 2013

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

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.