Skip to content

Commit

Permalink
go/storage/mkvs: Fix removal crash when key is too short
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Feb 19, 2020
1 parent c61be4f commit 3f83c17
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
1 change: 1 addition & 0 deletions .changelog/2698.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/storage/mkvs: Fix removal crash when key is too short
5 changes: 4 additions & 1 deletion go/storage/mkvs/urkel/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ func (t *tree) doRemove(

var changed bool
var existing []byte
if key.BitLength() == bitLength {
if key.BitLength() < bitLength {
// Lookup key is too short for the current n.Label, so it doesn't exist.
return ptr, false, nil, nil
} else if key.BitLength() == bitLength {
n.LeafNode, changed, existing, err = t.doRemove(ctx, n.LeafNode, bitLength, key, depth)
} else if key.GetBit(bitLength) {
n.Right, changed, existing, err = t.doRemove(ctx, n.Right, bitLength, key, depth+1)
Expand Down
31 changes: 13 additions & 18 deletions runtime/src/storage/mkvs/urkel/tree/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,27 @@ impl UrkelTree {
if let NodeBox::Internal(ref mut n) = *node_ref.borrow_mut() {
// Remove from internal node and recursively collapse the branch, if
// needed.
let (new_child, c, o) = if key.bit_length() == bit_depth + n.label_bit_length {
let bit_length = bit_depth + n.label_bit_length;

if key.bit_length() < bit_length {
// Lookup key is too short for the current n.Label, so it doesn't exist.
return Ok((ptr.clone(), false, None));
}

let (new_child, c, o) = if key.bit_length() == bit_length {
self._remove(ctx, n.leaf_node.clone(), bit_depth, key, depth)?
} else if key.get_bit(bit_depth + n.label_bit_length) {
self._remove(
ctx,
n.right.clone(),
bit_depth + n.label_bit_length,
key,
depth + 1,
)?
} else if key.get_bit(bit_length) {
self._remove(ctx, n.right.clone(), bit_length, key, depth + 1)?
} else {
self._remove(
ctx,
n.left.clone(),
bit_depth + n.label_bit_length,
key,
depth + 1,
)?
self._remove(ctx, n.left.clone(), bit_length, key, depth + 1)?
};

changed = c;
old_val = o;

if key.bit_length() == bit_depth + n.label_bit_length {
if key.bit_length() == bit_length {
n.leaf_node = new_child;
} else if key.get_bit(bit_depth + n.label_bit_length) {
} else if key.get_bit(bit_length) {
n.right = new_child;
} else {
n.left = new_child;
Expand Down

0 comments on commit 3f83c17

Please sign in to comment.