From: Michal 'vorner' Vaner Date: Thu, 19 Jul 2012 12:41:52 +0000 (+0200) Subject: [2090] Limit number of calls to get*() X-Git-Tag: trac2351_base~158^2~17^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=be46391e9694530d17277ebb5dea84f2d2616604;p=thirdparty%2Fkea.git [2090] Limit number of calls to get*() If they are called multiple times, it is stored into a variable. Segfaults, needs a fix (again). --- diff --git a/src/lib/datasrc/rbtree.h b/src/lib/datasrc/rbtree.h index 95e2586371..90e9d0fe81 100644 --- a/src/lib/datasrc/rbtree.h +++ b/src/lib/datasrc/rbtree.h @@ -419,8 +419,9 @@ RBNode::abstractSuccessor(typename RBNode::RBNodePtr RBNode::*left, // subtree. if (current->*right != RBNode::NULL_NODE()) { current = (current->*right).get(); - while (current->*left != RBNode::NULL_NODE()) { - current = (current->*left).get(); + const RBNode* left_n; + while ((left_n = (current->*left).get()) != RBNode::NULL_NODE()) { + current = left_n; } return (current); } @@ -1090,9 +1091,11 @@ RBTree::deleteHelper(RBNode* root) { RBNode* node = root; while (root->getLeft() != NULLNODE || root->getRight() != NULLNODE) { - while (node->getLeft() != NULLNODE || node->getRight() != NULLNODE) { - node = (node->getLeft() != NULLNODE) ? node->getLeft() : - node->getRight(); + RBNode* left(NULLNODE); + RBNode* right(NULLNODE); + while ((left = node->getLeft()) != NULLNODE || + (right = node->getRight()) != NULLNODE) { + node = (left != NULLNODE) ? left : right; } RBNode* parent = node->getParent(); @@ -1193,8 +1196,9 @@ RBTree::nextNode(RBTreeNodeChain& node_path) const { const RBNode* node = node_path.top(); // if node has sub domain, the next domain is the smallest // domain in sub domain tree - if (node->getDown() != NULLNODE) { - const RBNode* left_most = node->getDown(); + const RBNode* down = node->getDown(); + if (down != NULLNODE) { + const RBNode* left_most = down; while (left_most->left_ != NULLNODE) { left_most = left_most->getLeft(); } @@ -1265,11 +1269,12 @@ RBTree::previousNode(RBTreeNodeChain& node_path) const { node_path.push(current); // Go a level down and as much right there as possible current = current->getDown(); - while (current->getRight() != NULLNODE) { + const RBNode* right(NULLNODE); + while ((right = current->getRight()) != NULLNODE) { // A small trick. The current may be NULLNODE, but // such node has the right_ pointer and it is equal // to NULLNODE. - current = current->getRight(); + current = right; } } // Now, the one on top of the path is the one we want. We @@ -1343,12 +1348,14 @@ RBTree::previousNode(RBTreeNodeChain& node_path) const { node_path.push(node); // Try going as deep as possible, keeping on the right side of the trees - while (node->getDown() != NULLNODE) { + const RBNode* down; + while ((down = node->getDown()) != NULLNODE) { // Move to the tree below - node = node->getDown(); + node = down; // And get as much to the right of the tree as possible - while (node->getRight() != NULLNODE) { - node = node->getRight(); + const RBNode* right(NULLNODE); + while ((right = node->getRight()) != NULLNODE) { + node = right; } // Now, we found the right-most node in the sub-tree, we need to // include it in the path @@ -1470,39 +1477,43 @@ RBTree::insertRebalance(typename RBNode::RBNodePtr* root, RBNode* node) { RBNode* uncle; - while (node != *root && node->getParent()->color_ == RBNode::RED) { - if (node->getParent() == node->getParent()->getParent()->getLeft()) { - uncle = node->getParent()->getParent()->getRight(); + RBNode* parent; + while (node != *root && + (parent = node->getParent())->color_ == RBNode::RED) { + if (parent == parent->getParent()->getLeft()) { + uncle = parent->getParent()->getRight(); if (uncle->color_ == RBNode::RED) { - node->getParent()->color_ = RBNode::BLACK; + parent->color_ = RBNode::BLACK; uncle->color_ = RBNode::BLACK; - node->getParent()->getParent()->color_ = RBNode::RED; - node = node->getParent()->getParent(); + parent->getParent()->color_ = RBNode::RED; + node = parent->getParent(); } else { - if (node == node->getParent()->getRight()) { - node = node->getParent(); + if (node == parent->getRight()) { + node = parent; + parent = node->getParent(); leftRotate(root, node); } - node->getParent()->color_ = RBNode::BLACK; - node->getParent()->getParent()->color_ = RBNode::RED; - rightRotate(root, node->getParent()->getParent()); + parent->color_ = RBNode::BLACK; + parent->getParent()->color_ = RBNode::RED; + rightRotate(root, parent->getParent()); } } else { - uncle = node->getParent()->getParent()->getLeft(); + uncle = parent->getParent()->getLeft(); if (uncle->color_ == RBNode::RED) { - node->getParent()->color_ = RBNode::BLACK; + parent->color_ = RBNode::BLACK; uncle->color_ = RBNode::BLACK; - node->getParent()->getParent()->color_ = RBNode::RED; - node = node->getParent()->getParent(); + parent->getParent()->color_ = RBNode::RED; + node = parent->getParent(); } else { - if (node == node->getParent()->getLeft()) { - node = node->getParent(); + if (node == parent->getLeft()) { + node = parent; + parent = node->getParent(); rightRotate(root, node); } - node->getParent()->color_ = RBNode::BLACK; - node->getParent()->getParent()->color_ = RBNode::RED; - leftRotate(root, node->getParent()->getParent()); + parent->color_ = RBNode::BLACK; + parent->getParent()->color_ = RBNode::RED; + leftRotate(root, parent->getParent()); } } } @@ -1515,17 +1526,19 @@ template RBNode* RBTree::leftRotate(typename RBNode::RBNodePtr* root, RBNode* node) { RBNode* right = node->getRight(); - node->right_ = right->getLeft(); - if (right->getLeft() != NULLNODE) - right->getLeft()->parent_ = node; + RBNode* rleft = right->getLeft(); + node->right_ = rleft; + if (rleft != NULLNODE) + rleft->parent_ = node; - right->parent_ = node->getParent(); + RBNode* parent = node->getParent(); + right->parent_ = parent; - if (node->getParent() != NULLNODE) { - if (node == node->getParent()->getLeft()) { - node->getParent()->left_ = right; + if (parent != NULLNODE) { + if (node == parent->getLeft()) { + parent->left_ = right; } else { - node->getParent()->right_ = right; + parent->right_ = right; } } else { *root = right; @@ -1540,17 +1553,19 @@ template RBNode* RBTree::rightRotate(typename RBNode::RBNodePtr* root, RBNode* node) { RBNode* left = node->getLeft(); - node->left_ = left->getRight(); - if (left->getRight() != NULLNODE) - left->getRight()->parent_ = node; + RBNode* lright = left->getRight(); + node->left_ = lright; + if (lright != NULLNODE) + lright->parent_ = node; - left->parent_ = node->getParent(); + RBNode* parent = node->getParent(); + left->parent_ = parent; if (node->parent_ != NULLNODE) { - if (node == node->getParent()->getRight()) { - node->getParent()->right_ = left; + if (node == parent->getRight()) { + parent->right_ = left; } else { - node->getParent()->left_ = left; + parent->left_ = left; } } else { *root = left; @@ -1585,10 +1600,11 @@ RBTree::dumpTreeHelper(std::ostream& os, const RBNode* node, << ((node->color_ == RBNode::BLACK) ? "black" : "red") << ")"; os << ((node->isEmpty()) ? "[invisible] \n" : "\n"); - if (node->getDown() != NULLNODE) { + const RBNode* down = node->getDown(); + if (down != NULLNODE) { indent(os, depth + 1); os << "begin down from " << node->name_.toText() << "\n"; - dumpTreeHelper(os, node->getDown(), depth + 1); + dumpTreeHelper(os, down, depth + 1); indent(os, depth + 1); os << "end down from " << node->name_.toText() << "\n"; }