Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Delete(...) early return #425

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ func (n *InternalNode) Delete(key []byte, resolver NodeResolverFn) (bool, error)
// as well.
for _, c := range n.children {
if _, ok := c.(Empty); !ok {
break
return false, nil
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While breaking was reasonable, the right thing is to return false. Only break would mean L625 being executed signaling that a node deletion should happen, when that shouldn't be the case.

}
}

Expand Down
33 changes: 33 additions & 0 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1806,3 +1806,36 @@ func TestRandom(t *testing.T) {
t.Fatal(err)
}
}

func TestRandomExtracted(t *testing.T) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So after a while of debugging TestRandom(...) I extracted (and manually "minified") a simple test that surfaced the bug. Leaving the test here to avoid regressions.

Interestingly, this bug wasn't affecting this other test that was targeting this API.

t.Parallel()

k1490, _ := hex.DecodeString("1490d65886f02ffa1a7dca88187dc8176ada23221c83e9f7da9e15c27d0b813d")
val_k1490_0, _ := hex.DecodeString("3a8ee0370c16ba642123f80692ee63ade4477fce3bf0c68b638e189a014d8b04")
k1413, _ := hex.DecodeString("1413dabef075cc47d380d740f7b24050568659c809830b1affbc765b7b651e1e")
val_k1413_0, _ := hex.DecodeString("7469fa29e0d049e80eed8f99f2418bc36ebc3c3b3041515fd519701d60f86b9f")
val_k1413_1, _ := hex.DecodeString("79702b187fa6f75ecd7cad23a2cafc3b13976dcefd99c3ff479300655ef9ef92")

root := New()

if err := root.Insert(k1490, val_k1490_0, nil); err != nil {
panic(err)
}
if err := root.Insert(k1413, val_k1413_0, nil); err != nil {
panic(err)
}
if _, err := root.Delete(k1413, nil); err != nil {
panic(err)
}
if err := root.Insert(k1413, val_k1413_1, nil); err != nil {
panic(err)
}

val, err := root.Get(k1490, nil)
if err != nil {
t.Fatalf("error getting key: %v", err)
}
if !bytes.Equal(val, val_k1490_0) {
t.Fatalf("got %x, expected %x", val, val_k1490_0)
}
}
Loading