From ace5f9ead8c72f74616f96f7fd396b77391247d9 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 18 Jan 2013 14:49:19 +0100 Subject: Make sure we don't create cycle in the tree while moving... --- src/tasktree.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/tasktree.cc b/src/tasktree.cc index c463177..39cee98 100644 --- a/src/tasktree.cc +++ b/src/tasktree.cc @@ -173,6 +173,17 @@ TaskIdList TaskTree::remove(taskid_t id) return affectedNodes; } +static bool findNode(node_t *needle, node_t *haystack) +{ + if(needle == haystack) return true; + NodeList::iterator i = haystack->children.begin(); + while(i != haystack->children.end()) { + if(findNode(needle, *i)) return true; + i++; + } + return false; +} + TaskIdList TaskTree::move(taskid_t id, taskid_t toid) throw (std::exception) { @@ -183,6 +194,9 @@ TaskIdList TaskTree::move(taskid_t id, taskid_t toid) node_t* child = id2node.at(id); node_t* newparent = id2node.at(toid); + // Test if new parent is a child of the node itself... + if(findNode(newparent, child)) throw std::exception(); + if(!child->parent) { throw std::exception(); } -- cgit v1.2.3