Skip to content

Commit

Permalink
Fix reference counter falling belowing zero
Browse files Browse the repository at this point in the history
This commit fixes an issue where a node's reference counter
may fall below zero when more than one tree share the same node
(rc == 1) and then the both copy the node and fetch_subtract the
rc. This is a hard to produce issue, but possible.

The rc must never fall below zero except when the node is
about to be freed by the node_free function.

This commit replaces the fetch_subtract with node_free which
correctly frees the node when the rc == -1.
  • Loading branch information
tidwall committed May 3, 2024
1 parent cf1bb92 commit c0cfc4e
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ static struct btree_node *btree_node_copy(struct btree *btree,
if (btree_rc_load(&(bnode)->rc) > 0) { \
struct btree_node *node2 = btree_node_copy(btree, (bnode)); \
if (!node2) { code; } \
btree_rc_fetch_sub(&(bnode)->rc, 1); \
btree_node_free(btree, bnode); \
(bnode) = node2; \
} \
}
Expand Down

0 comments on commit c0cfc4e

Please sign in to comment.