From a4a1cd107223ff275b83d956430b64fdb9594f4c Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sat, 9 Jun 2012 15:43:36 +0200 Subject: [PATCH] BUG/MEDIUM: ebtree: ebmb_insert() must not call cmp_bits on full-length matches Otherwise we end up comparing the byte past the end, resulting in duplicate values still being inserted into the tree even if undesired. This generally has low impact, though it can sometimes cause one new entry to be added next to an existing one for stick tables, preventing the results from being merged. (cherry picked from commit 12e54ac493a91bb02064568f410592c2700d3933) --- ebtree/ebimtree.h | 18 ++++++++++++++++-- ebtree/ebmbtree.h | 14 +++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/ebtree/ebimtree.h b/ebtree/ebimtree.h index b40e490b4..1fe9fa0ac 100644 --- a/ebtree/ebimtree.h +++ b/ebtree/ebimtree.h @@ -198,7 +198,14 @@ __ebim_insert(struct eb_root *root, struct ebpt_node *new, unsigned int len) * The last two cases can easily be partially merged. */ bit = equal_bits(new->key, old->key, bit, len); - diff = cmp_bits(new->key, old->key, bit); + + /* Note: we can compare more bits than the current node's because as + * long as they are identical, we know we descend along the correct + * side. However we don't want to start to compare past the end. + */ + diff = 0; + if (((unsigned)bit >> 3) < len) + diff = cmp_bits(new->key, old->key, bit); if (diff < 0) { new->node.leaf_p = new_left; @@ -263,7 +270,14 @@ __ebim_insert(struct eb_root *root, struct ebpt_node *new, unsigned int len) new->node.node_p = old->node.node_p; - diff = cmp_bits(new->key, old->key, bit); + /* Note: we can compare more bits than the current node's because as + * long as they are identical, we know we descend along the correct + * side. However we don't want to start to compare past the end. + */ + diff = 0; + if (((unsigned)bit >> 3) < len) + diff = cmp_bits(new->key, old->key, bit); + if (diff < 0) { new->node.leaf_p = new_left; old->node.node_p = new_rght; diff --git a/ebtree/ebmbtree.h b/ebtree/ebmbtree.h index 121c59fa1..fa25ac58a 100644 --- a/ebtree/ebmbtree.h +++ b/ebtree/ebmbtree.h @@ -306,12 +306,16 @@ __ebmb_insert(struct eb_root *root, struct ebmb_node *new, unsigned int len) new_rght = eb_dotag(&new->node.branches, EB_RGHT); new_leaf = eb_dotag(&new->node.branches, EB_LEAF); - /* Note: we can compare more bits than - * the current node's because as long as they are identical, we - * know we descend along the correct side. - */ new->node.bit = bit; - diff = cmp_bits(new->key, old->key, bit); + + /* Note: we can compare more bits than the current node's because as + * long as they are identical, we know we descend along the correct + * side. However we don't want to start to compare past the end. + */ + diff = 0; + if (((unsigned)bit >> 3) < len) + diff = cmp_bits(new->key, old->key, bit); + if (diff == 0) { new->node.bit = -1; /* mark as new dup tree, just in case */