Skip to content
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

Global unique node names #9

Merged
merged 1 commit into from
Aug 23, 2012
Merged

Global unique node names #9

merged 1 commit into from
Aug 23, 2012

Conversation

ysf
Copy link
Contributor

@ysf ysf commented Aug 23, 2012

While using RubyTree at work I noticed that the node names, though the documentation states "The node name is expected to be unique within the tree.", are not unique if you add an already added node/name to another child. There are several ways to fix this, the easiest implementation, is to check if the tree, where the child is added too, already contains a node with this name.

Because I'm not allowed to spend too much worktime on open source projects I stuck with the simple implementation I mentioned and hope that this is of any use to you anyway. While testing, the "add node to self as child" case was fixed too so I changed the test likewise.

Adding a node might still be exploitable by something like this, I hope you get the idea, could happen anytime you add a node or merge two trees:

@root     = Tree::TreeNode.new('root', "Root node")
@child1   = Tree::TreeNode.new('child1', '1st child')
@child2   = Tree::TreeNode.new('child2', '2nd child')
@conflict = Tree::TreeNode.new('conflict', 'Should do problems')

@child1 << @conflict # should work because @conflict is not in root-tree
@child2 << @conflict # same here.

@root << @child1 # no problem so far
@root << @child2 # now two nodes with the name 'conflict' exist in root tree

Thanks a lot for RubyTree and best regards, ysf :)

@travisbot
Copy link

This pull request fails (merged 76b6d24 into a76aec1).

@ghost ghost assigned evolve75 Aug 23, 2012
raise "Child #{child.name} already added!" if @children_hash.has_key?(child.name)

# Lazy mans unique test, won't test if children of child are unique in this tree too.
self.root.each { |node| raise "Child #{child.name} already added!" if node.name == child.name }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to make this configurable. The issue is that while this is the correct behavior (in terms of the assertion/check), there is existing code which is using duplicate node names already, and this fix will break that code.

evolve75 added a commit that referenced this pull request Aug 23, 2012
Global unique node names
@evolve75 evolve75 merged commit 3af3a3a into evolve75:master Aug 23, 2012
evolve75 added a commit that referenced this pull request Dec 27, 2013
Global unique node names
@sirsean sirsean mentioned this pull request Jan 23, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants