facebook / hhvm

A virtual machine for executing programs written in Hack.
https://hhvm.com
Other
18.18k stars 2.99k forks source link

SplDoublyLinkedList: clone's behaviour should mimic PHP5 #6802

Open afk11 opened 8 years ago

afk11 commented 8 years ago

Discovered an issue in HHVM's reimplementation of SplDoublyLinkedList. It appears because it relies on SplDoublyLinkedNode's, a deep clone is required to prevent the clone from being mutated post creation.

In PHP, the SplDoublyLinkedList class comes from the SPL extension written in C. I suspect the divergence comes from reimplementing the class in PHP/HHVM, and cloning without special handling (since the internals are totally different)

I will follow up with a pull request 'fixing this' as best I see it. Raising the issue to remind me and to request feedback on this.

jesseschalken commented 7 years ago

Example:

$list1 = new \SplDoublyLinkedList();

$list1->push(1);
$list1->push(2);

$list2 = clone $list1;
$list2->push(3);
$list2->push(4);

var_export(iterator_to_array($list1));
print "\n";
var_export(iterator_to_array($list2));
print "\n";

PHP:

array (
  0 => 1,
  1 => 2,
)
array (
  0 => 1,
  1 => 2,
  2 => 3,
  3 => 4,
)

HHVM:

array (
  0 => 1,
  1 => 2,
  2 => 3,
  3 => 4,
)
array (
  0 => 1,
  1 => 2,
  2 => 3,
  3 => 4,
)

https://3v4l.org/n1v5j

lexidor commented 1 year ago

Thank you for the repro. This no isolated repro tag could have been removed immediately after you replied.

This is indeed quite silly. PHP's behavior is the only one that makes sense here. I expect list1 and list2 to be independent objects after the clone.