Skip to content

Commit

Permalink
Copy fewer nodes on rebalance
Browse files Browse the repository at this point in the history
This commit fixes a potential future memory error.

It can't be currently tested in production because the memmove
overwrites a "spare" slot that isn't used during the
rebalance operation. But without this fix, if at some point in
the future the spare slots are no longer located immediately
after the child nodes then a heap-buffer-overflow would have
happen.
  • Loading branch information
tidwall committed May 11, 2024
1 parent c0cfc4e commit 10d69a3
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// license that can be found in the LICENSE file.

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -162,6 +163,7 @@ static void btree_node_shift_left(struct btree *btree, struct btree_node *node,
if (!node->leaf) {
if (for_merge) {
index++;
num_items_to_shift--;
}
memmove(&node->children[index], &node->children[index+1],
(num_items_to_shift+1)*sizeof(struct btree_node*));
Expand Down

0 comments on commit 10d69a3

Please sign in to comment.