PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Structures » Structures_Graph » Bug #2536

Bug in Structures_graph::addNode

Details

Submitted2004-10-15 14:49 UTC
Fromotiteca at free dot fr
Assignedsergiosgc
StatusBogus
PackageStructures_Graph
PHP Version4.3.4
Roadmaps(Not assigned)

Comments

[2004-10-15 14:49 UTC] otiteca at free dot fr

Description:
------------
Hi,

In class Structures_Graph, function addNode(&$newNode),
you expect to avoid adding same references.

But, your test seems wrong to me:

$savedData = $this->_nodes[$key];
$referenceIsEqualFlag = false;
$this->_nodes[$key] = true;
if ($node === true) {
$this->_nodes[$key] = false;
if ($node === false)
$referenceIsEqualFlag = true;
}
$this->_nodes[$key] = $savedData;
if ($referenceIsEqualFlag)
Pear::raiseError('Structures_Graph::addNode received an object that is a duplicate for this dataset', STRUCTURES_GRAPH_ERROR_GENERIC);
}

in fact, in the test: if ($node === true) will always be false then the block will ever execute.

Certainly you do have to replace by: if ($newNode === true),
what means that the current node in the graph is the same(reference) as the node you want to insert, then rase the error.

Rgs,

Olivier

Reproduce code:
---------------
// it's possible to had 2 nodes with same reference:

$toto =& new Strucutres_Graph();
$mynode = new Structures_Graph_Node();
$mynode->setData("ptptptpttptptptptp");

$toto->addNode($mynode);
$toto->addNode($mynode); // no error

print_r($toto->_nodes); // 2 nodes, and the same reference...

[2004-10-20 13:53 UTC] sergio dot carvalho at portugalmail dot com

A first review seems to indicate this is really a bug. I'll look into it shortly. Removed the OS entry, since specific OS should be irrelevant to this bug.

[2005-12-09 11:52 UTC] sergio dot carvalho at portugalmail dot com

This is not a bug, although the code is convoluted. What this code does is change the $this->_nodes[$key] reference, and check if the changes reflect in $node. If they do, then the two references point to the same variable.

[2005-12-09 11:54 UTC] sergiosgc at php dot net

Thank you for taking the time to write to us, but this is not
a bug.

The code is indeed strange, but it does what is expected. I've added a small comment better explaining what it does --
it took me a few minutes to understand my own code :-P