Skip to content

Prevent spl_object_hash collisions #1430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,21 @@ class UnitOfWork implements PropertyChangedListener
private $persistenceBuilder;

/**
* Array of parent associations between embedded documents
* Array of parent associations between embedded documents.
*
* @todo We might need to clean up this array in clear(), doDetach(), etc.
* @var array
*/
private $parentAssociations = array();

/**
* Array of embedded documents known to UnitOfWork. We need to hold them to prevent spl_object_hash
* collisions in case already managed object is lost due to GC (so now it won't). Embedded documents
* found during doDetach are removed from the registry, to empty it altogether clear() can be utilized.
*
* @var array
*/
private $embeddedDocumentsRegistry = array();

/**
* @var LifecycleEventManager
*/
Expand Down Expand Up @@ -292,6 +300,7 @@ public function getPersistenceBuilder()
public function setParentAssociation($document, $mapping, $parent, $propertyPath)
{
$oid = spl_object_hash($document);
$this->embeddedDocumentsRegistry[$oid] = $document;
$this->parentAssociations[$oid] = array($mapping, $parent, $propertyPath);
}

Expand Down Expand Up @@ -1984,7 +1993,7 @@ private function doDetach($document, array &$visited)
$this->documentDeletions[$oid], $this->documentIdentifiers[$oid],
$this->documentStates[$oid], $this->originalDocumentData[$oid],
$this->parentAssociations[$oid], $this->documentUpserts[$oid],
$this->hasScheduledCollections[$oid]);
$this->hasScheduledCollections[$oid], $this->embeddedDocumentsRegistry[$oid]);
break;
case self::STATE_NEW:
case self::STATE_DETACHED:
Expand Down Expand Up @@ -2282,6 +2291,7 @@ public function clear($documentName = null)
$this->collectionUpdates =
$this->collectionDeletions =
$this->parentAssociations =
$this->embeddedDocumentsRegistry =
$this->orphanRemovals =
$this->hasScheduledCollections = array();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Doctrine\ODM\MongoDB\Tests\Functional;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Tests\BaseTest;

class SplObjectHashCollisionsTest extends BaseTest
{
/**
* @dataProvider provideParentAssociationsIsCleared
*/
public function testParentAssociationsIsCleared($f)
{
$d = new SplColDoc();
$d->one = new SplColEmbed('d.one.v1');
$d->many[] = new SplColEmbed('d.many.0.v1');
$d->many[] = new SplColEmbed('d.many.1.v1');

$this->dm->persist($d);
$this->expectCount('parentAssociations', 3);
$this->expectCount('embeddedDocumentsRegistry', 3);
$f($this->dm, $d);
$this->expectCount('parentAssociations', 0);
$this->expectCount('embeddedDocumentsRegistry', 0);
}

/**
* @dataProvider provideParentAssociationsIsCleared
*/
public function testParentAssociationsLeftover($f, $leftover)
{
$d = new SplColDoc();
$d->one = new SplColEmbed('d.one.v1');
$d->many[] = new SplColEmbed('d.many.0.v1');
$d->many[] = new SplColEmbed('d.many.1.v1');
$this->dm->persist($d);
$d->one = new SplColEmbed('d.one.v2');
$this->dm->flush();

$this->expectCount('parentAssociations', 4);
$this->expectCount('embeddedDocumentsRegistry', 4);
$f($this->dm, $d);
$this->expectCount('parentAssociations', $leftover);
$this->expectCount('embeddedDocumentsRegistry', $leftover);
}

public function provideParentAssociationsIsCleared()
{
return [
[ function (DocumentManager $dm) { $dm->clear(); }, 0 ],
[ function (DocumentManager $dm, $doc) { $dm->clear(get_class($doc)); }, 1 ],
[ function (DocumentManager $dm, $doc) { $dm->detach($doc); }, 1 ],
];
}

private function expectCount($prop, $expected)
{
$ro = new \ReflectionObject($this->uow);
$rp = $ro->getProperty($prop);
$rp->setAccessible(true);
$this->assertCount($expected, $rp->getValue($this->uow));
}
}

/** @ODM\Document */
class SplColDoc
{
/** @ODM\Id */
public $id;

/** @ODM\Field(type="string") */
public $name;

/** @ODM\EmbedOne */
public $one;

/** @ODM\EmbedMany */
public $many = [];
}

/** @ODM\EmbeddedDocument */
class SplColEmbed
{
/** @ODM\Field(type="string") */
public $name;

public function __construct($name)
{
$this->name = $name;
}
}