php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60560 SplFixedArray un-/serialize, getSize(), count() return 0, keys are strings
Submitted: 2011-12-19 10:31 UTC Modified: 2012-02-21 10:34 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: digital1kk at interia dot pl Assigned: aharvey (profile)
Status: Closed Package: SPL related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2011-12-19 10:31 UTC] digital1kk at interia dot pl
Description:
------------
If SplFixedArray object is created via unserialize() of serialized instance then the getSize() and count() methods return 0 instead of expected actual size.

Tested on Debian Squeeze 6 64bit:
x86-5.4.0RC3-dev
Tested on windows 7 64bit:
x86-5.4.0RC3-nts
x86-5.4.0trunk(12.12)-nts
x86-5.4.0snap(13.12)-nts
x86-5.3.8-nts
x86-5.3.6-ts
x86-5.3.6-nts
x64-5.3.6-nts
x86-5.3.4-nts
x86-5.3.1-ts

Test script:
---------------
<?php
$a = new SplFixedArray(2);
$a[0] = 1; 
$a[1] = 2;
echo '$a = ', var_dump($a);
echo 'Sizeof $a = ' . $a->getSize(), PHP_EOL;
echo 'Count  $a = ' . $a->count(),   PHP_EOL;
echo PHP_EOL;
$b = unserialize(serialize($a)); 
echo '$b = ', var_dump($b);
echo 'Sizeof $b = ' . $b->getSize(), PHP_EOL;
echo 'Count  $b = ' . $b->count(),   PHP_EOL;

Expected result:
----------------
$a = object(SplFixedArray)#1 (2) {
  [0]=>
  int(1)
  [1]=>
  int(2)
}
Sizeof $a = 2
Count  $a = 2

$b = object(SplFixedArray)#2 (2) {
  [0]=>
  int(1)
  [1]=>
  int(2)
}
Sizeof $b = 2
Count  $b = 2

Actual result:
--------------
$a = object(SplFixedArray)#1 (2) {
  [0]=>
  int(1)
  [1]=>
  int(2)
}
Sizeof $a = 2
Count  $a = 2

$b = object(SplFixedArray)#2 (2) {
  [0]=>
  int(1)
  [1]=>
  int(2)
}
Sizeof $b = 0
Count  $b = 0

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-12-19 13:49 UTC] digital1kk at interia dot pl
-Summary: SplFixedArray getSize(), count() after unserialization return 0 +Summary: SplFixedArray un-/serialize, getSize(), count() return 0, keys are strings
 [2011-12-19 13:49 UTC] digital1kk at interia dot pl
Quick fix is to store in serialized form internal array:
-------------------------------------
$sa = serialize($a->toArray());
$ua = unserialize($sa);
$b = SplFixedArray::fromArray($ua); 
var_dump($b);
echo 'Sizeof $b = ' . $b->getSize(), PHP_EOL;
echo 'Count  $b = ' . $b->count(),   PHP_EOL;
-------------------------------------
Gives the expected results

Also I forgot in php >= 5.4.0RC3 (should I report this as separate bug?)
The actual result of var_dump($b) is:
$b = object(SplFixedArray)#2 (2) {
  ["0"]=>
  int(1)
  ["1"]=>
  int(2)
}
The keys are strings and not integers and this causes
-------------------------------------
$b = unserialize(serialize($a));
SplFixedArray::fromArray($b->toarray()); 
-------------------------------------
To throw an exception 'InvalidArgumentException' with message 'array must contain only positive integer keys'
 [2012-02-21 10:34 UTC] aharvey@php.net
Automatic comment from SVN on behalf of aharvey
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=323408
Log: Add a __wakeup() method to SplFixedArray, thereby fixing serialising an
SplFixedArray object and bug #60560 (SplFixedArray un-/serialize, getSize(),
count() return 0, keys are strings).
 [2012-02-21 10:34 UTC] aharvey@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: aharvey
 [2012-02-21 10:34 UTC] aharvey@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

Fixed on trunk.
 [2012-06-18 15:57 UTC] php at maisqi dot com
Hello. I'm experiencing this problem in Windows and Linux, PHP 5.4.4 and PHP 5.3.3. I argue it's not resolved again.
I wrote a demo at http://maisqi.com/outros/bugs/php/SplFixedArraySerialization and I'd appreciate if someone else makes his own tests. Here is the demo source code:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SplFixedArray Serialization</title>
</head>
<body>

<p>
	An unserialized SplFixedArray loses it's element count, though it keeps the
	elements. This seems to be
	<a href="https://bugs.php.net/bug.php?id=60560&amp;edit=1">Bug #60560</a> all
	over again.
</p>
<p>
	This demo runs on a PHP 5.3.3 CGI under Apache and Linux. Also tested it
	on a PHP 5.4.4 running as a Apache 2 module under Windows 7 32 bits, with
	same results.
</p>

<hr />
<pre>
<?php

$fn = 'serial.txt';
$x = new SplFixedArray (4);
$x [0] = 123;
$x [1] = 456;
$x [2] = 789;
$x [3] = 'abc';
echo "<strong>SplFixedArray to serialize</strong>:\n";
print_r ($x);
echo "\ncount: <strong>", $x->getSize (), "</strong>";

echo "\n<strong>First element</strong>: ";
try {	$o = $x [0];	} catch (Exception $e) {	$o = 'Error!';	}
echo "<strong>$o</strong>\n";


echo "\n\nSerializing to file \"$fn\"...\n";
file_put_contents ($fn, serialize ($x));

echo "\n\nUnserializing from file \"$fn\"...\n";
$x = unserialize (file_get_contents ($fn));

echo "\n\n<strong>Unserialized SplFixedArray</strong>:\n";
print_r ($x);
echo "\ncount: <strong>", $x->getSize (), "</strong>";

echo "\n<strong>First element</strong>: ";
try {	$o = $x [0];	} catch (Exception $e) {	$o = 'Error!';	}
echo "<strong>$o</strong>\n";


if ($buf = @file_get_contents ('error_log')) {
	echo "\n\n<strong>Last errors</strong>:\n", $buf;
}

?>
</pre>
</body>
</html>
 [2012-08-03 14:36 UTC] php at maisqi dot com
Hello. This is not really fixed. I still got this error on PHP 5.4.5. I don't believe that the said fix wasn't released in six months, so something is wrong.

So, is this going to be reopen or should I file a new bug?
 [2013-06-17 22:51 UTC] rewilliams at newtekemail dot com
It appears this is fixed in 5.5 but not 5.4.x, at least not as of 5.4.13, which is 
the latest to which I have access.

Is there any chance the fix will be back-ported to 5.4?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC