php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65440 _id added to the initial array without checking the "copy on write"
Submitted: 2013-08-12 16:37 UTC Modified: 2014-02-09 18:58 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: gerald at croes dot org Assigned:
Status: Not a bug Package: mongo (PECL)
PHP Version: 5.5.1 OS: Linux
Private report: No CVE-ID: None
 [2013-08-12 16:37 UTC] gerald at croes dot org
Description:
------------
I understood that the Mongo drivers are adding the _id property to the given array 
if the array is given by copy. This is all good.

But the Mongo drivers are not checking if the given array is itself a copy of an 
unmodified array.

By forgetting this check, it updates the initial array and its copy.

In the given test script, if I add an unset($toInsert['fooProperty']) it works as 
expected (as the array copy is now effective and different than its origin).

Test script:
---------------
<?php
class EntityObject {
    private $arData = array();
 
    public function __construct ($initialValue) {
        $this->setProperty($initialValue);
    }
 
    public function setProperty ($value) {
        $this->arData['property'] = $value;
    }
 
    public function getProperties () {
        return $this->arData;
    }
}
$entity = new EntityObject('foo');
 
$mongoClient = new MongoClient();
$toInsert = $entity->getProperties();
$mongoClient->fooDb->fooCollection->insert($toInsert, array('fsync'=>true));
 
echo "toInsert should be updated with _id \n";
print_r($toInsert);
 
echo "private data should not be updated with _id \n";
print_r($entity->getProperties());

Expected result:
----------------
toInsert should be updated with _id 
Array
(
    [property] => foo
    [_id] => MongoId Object
        (
            [$id] => 52090a28945630cf261e34fc
        )
)
private data should not be updated with _id 
Array
(
    [property] => foo
)
 

Actual result:
--------------
toInsert should be updated with _id 
Array
(
    [property] => foo
    [_id] => MongoId Object
        (
            [$id] => 52090a28945630cf261e34fc
        )
 
)
private data should not be updated with _id 
Array
(
    [property] => foo
    [_id] => MongoId Object
        (
            [$id] => 52090a28945630cf261e34fc
        )
 
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-08-12 16:52 UTC] gerald at croes dot org
BTW, checked against 1.4.2 and github (bc6998deffbdd3d1ba3b324f1488c7414a479c57)
 [2014-02-09 18:58 UTC] bjori@php.net
-Status: Open +Status: Not a bug
 [2014-02-09 18:58 UTC] bjori@php.net
There isn't actually a whole lot we can do about this at this time.

We shouldn't have been modifying the argument at all, rather return the generated ID for example.. But that ship has sailed and people are relying on this behavior now...

Suddenly not updating with the _id could be catastrophic for applications, so this is not something we are willing to mess around with.


Maybe, hopefully, we'll change the API in 2.0 and come up with something better - completely breaking this in an obvious way, rather then breaking it in a subtle you-will-never-notice-until-its-to-late way
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Jul 18 04:01:25 2019 UTC