internal/packing: bug fix: node's links were not synced correctly

Closes #2584
This commit is contained in:
Hajime Hoshi 2023-02-28 17:06:51 +09:00
parent 74210b0425
commit 9e0bebda71
2 changed files with 48 additions and 2 deletions

View File

@ -245,7 +245,8 @@ func (p *Page) Extend(count int) bool {
}
if aborted {
origRoot := *p.root
origRoot := p.root
origRootCloned := *p.root
leftUpper := p.root
leftLower := &Node{
@ -285,7 +286,10 @@ func (p *Page) Extend(count int) bool {
origSize := p.size
p.rollbackExtension = func() {
p.size = origSize
p.root = &origRoot
// The node address must not be changed, so use the original root node's pointer (#2584).
// As the root node might be modified, recover the content from the cloned content.
p.root = origRoot
*p.root = origRootCloned
}
} else {
origSize := p.size

View File

@ -359,3 +359,45 @@ func TestExtendWithoutAllocation(t *testing.T) {
t.Errorf("p.Size(): got: %d, want: %d", got, want)
}
}
// Issue #2584
func TestRemoveAtRootsChild(t *testing.T) {
p := packing.NewPage(32, 1024)
alloc := func(width, height int) *packing.Node {
n := p.Alloc(width, height)
if n != nil {
return n
}
for i := 1; i < 100; i++ {
if !p.Extend(i) {
t.Fatalf("p.Extend(%d) failed", i)
}
if n = p.Alloc(width, height); n != nil {
p.CommitExtension()
return n
}
p.RollbackExtension()
}
t.Fatalf("never reached")
return nil
}
n0 := alloc(18, 18)
n1 := alloc(28, 59)
n2 := alloc(18, 18)
n3 := alloc(18, 18)
n4 := alloc(8, 10)
n5 := alloc(322, 242)
_ = n5
p.Free(n0)
p.Free(n2)
p.Free(n1)
p.Free(n3)
p.Free(n4)
n6 := alloc(18, 18)
p.Free(n6)
}