Bug #74586 [NEW]: Arrays and objects with duplicate property/key names (serialization bug)

From: Date: Sat, 13 May 2017 11:53:29 +0000
Subject: Bug #74586 [NEW]: Arrays and objects with duplicate property/key names (serialization bug)
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-209095@lists.php.net to get a copy of this message
From:             daniil at daniil dot it
Operating system: 
PHP version:      Irrelevant
Package:          *Programming Data Structures
Bug Type:         Bug
Bug description:Arrays and objects with duplicate property/key names (serialization bug)

Description:
------------
Deserializing a class that has attributes with access modifiers
different from the ones used during deserialization creates attributes
with duplicate attribute names, and converting such objects to arrays
using get_object_vars creates arrays with duplicate keys.

This can also be considered a memory leak, since the attributes with the
old access modifiers cannot be unset or modified.

Test script:
---------------
// a.php
<?php
class a {
    public $a = 'public';
    public function dump() { return get_object_vars($this); }
}
$a = new a();
file_put_contents('test', serialize($a));
var_dump($a, $a->dump());

// b.php
<?php
class a {
    protected $a = 'protected';
    public function dump() { return get_object_vars($this); }
}
$a = unserialize(file_get_contents('test'));
file_put_contents('test', serialize($a));
var_dump($a, $a->dump());

// c.php
<?php

class a {
    private $a = 'private';
    public function dump() { return get_object_vars($this); }
    public function adump() { return (array)$this; }
    public function vdump() { foreach ($this as $key => $value) {
var_dump($key.' => '.$value); } }
    public function unset() { var_dump('UNSETTING $this->a');
unset($this->a); }
}
$a = unserialize(file_get_contents('test'));
file_put_contents('test', serialize($a));
$array = $a->dump();
var_dump($a, $array, $array['a']);

var_dump('UNSETTING $array["a"]');
unset($array['a']);
var_dump($array);

var_dump('UNSETTING $array["a"]');
unset($array['a']);
var_dump($array);

var_dump('UNSETTING $array["a"]');
unset($array['a']);
var_dump($array);


var_dump((object)$a->adump());

$a->vdump();
$a->unset(); // The private property is being unset
$a->vdump();

$a->unset(); // Nothing is being unset
$a->vdump();



Expected result:
----------------
// php a.php && php b.php && php c.php

// a.php
object(a)#1 (1) {
  ["a"]=>
  string(6) "public"
}
array(1) {
  ["a"]=>
  string(6) "public"
}

// b.php
object(a)#1 (2) {
  ["a":protected]=>
  string(9) "protected"
  ["a"]=>
  string(6) "public"
}
array(2) { // NICE
  ["a"]=>
  string(9) "protected"
  ["a"]=>
  string(6) "public"
}

// c.php
object(a)#1 (3) {
  ["a":"a":private]=>
  string(7) "private"
  ["a":protected]=>
  string(9) "protected"
  ["a"]=>
  string(6) "public"
}
array(3) {
  ["a"]=>
  string(7) "private"
  ["a"]=>
  string(9) "protected"
  ["a"]=>
  string(6) "public"
}

string(6) "public" // The last element is always fetched/unset/modified

string(21) "UNSETTING $array["a"]"
array(2) {
  ["a"]=>
  string(7) "private"
  ["a"]=>
  string(9) "protected"
}
string(21) "UNSETTING $array["a"]"
array(1) {
  ["a"]=>
  string(7) "private"
}
string(21) "UNSETTING $array["a"]"
array(0) {
}
object(stdClass)#2 (3) { // Since the array generated using the array
cast contains the access modifier indicators (private variables have the
class name prepended to the variable name; protected variables have a
'*' prepended to the variable name.), casting that array back to an
object works (so the behaviour can be replicated manually without
serialize)
  ["a":"a":private]=>
  string(7) "private"
  ["a":protected]=>
  string(9) "protected"
  ["a"]=>
  string(6) "public"
}
string(12) "a => private"
string(14) "a => protected"
string(11) "a => public"
string(18) "UNSETTING $this->a"
string(14) "a => protected"
string(11) "a => public"
string(18) "UNSETTING $this->a"
string(14) "a => protected"
string(11) "a => public"



-- 
Edit bug report at https://bugs.php.net/bug.php?id=74586&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=74586&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=74586&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=74586&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=74586&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=74586&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=74586&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=74586&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=74586&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=74586&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=74586&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=74586&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=74586&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=74586&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=74586&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=74586&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=74586&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=74586&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=74586&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=74586&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=74586&r=mysqlcfg



Thread (2 messages)

« previous php.bugs (#209095) next »