php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51173 Unserialized objects with prototype changes results in duplicated properties
Submitted: 2010-02-28 11:12 UTC Modified: 2010-02-28 12:47 UTC
From: seld@php.net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.2RC3 OS: Win7
Private report: No CVE-ID: None
 [2010-02-28 11:12 UTC] seld@php.net
Description:
------------
If you serialize an object in the session and then change the properties declaration from public to protected (or any other change), the unserialized objects will have duplicated properties, once public and once protected, and the object will not be able to read the public value so it means the object state is broken.

This is obviously due to the way the protected values are stored as "\0*\0property" => "value", but I still think the default unserializer code should check if "property" exists, and fill it no matter what the access level is, rather than creating another property with the same name. I don't know how easy it'd be and how it would impact performance though, but it's quite scary that the engine even allows having two properties with different access levels and the same name.

Reproduce code:
---------------
// Run once like this, then change public $property to protected $property, run again and look at var_dump output

session_start();

class testClass {
  public $property;
  public function __construct($val) {
    $this->property = $val;
  }
}

if (!isset($_SESSION['obj'])) {
  $_SESSION['obj'] = new testClass('value');
}

var_dump($_SESSION['obj']);


Expected result:
----------------
object(testClass)[1]
  protected 'property' => string 'value' (length=5)

Actual result:
--------------
object(testClass)[1]
  protected 'property' => null
  public 'property' => string 'value' (length=5)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-02-28 12:47 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

Fixing this creates more issues, like performance drop, than it solves.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 17 19:01:30 2024 UTC