From 6b957063d46e4460c2a2b944b419fc6f0f9c5164 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 28 Feb 2023 17:06:51 +0900 Subject: [PATCH] internal/packing: bug fix: node's links were not synced correctly Closes #2584 --- internal/packing/packing.go | 8 ++++++-- internal/packing/packing_test.go | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/internal/packing/packing.go b/internal/packing/packing.go index b2e69d57d..fd7ded949 100644 --- a/internal/packing/packing.go +++ b/internal/packing/packing.go @@ -318,7 +318,8 @@ func (p *Page) extend(newWidth int, newHeight int) func() { var rollback func() if aborted { - origRoot := *p.root + origRoot := p.root + origRootCloned := *p.root // Extend the page in the vertical direction. if newHeight-p.height > 0 { @@ -366,7 +367,10 @@ func (p *Page) extend(newWidth int, newHeight int) func() { rollback = func() { p.width = origWidth p.height = origHeight - 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 { origWidth, origHeight := p.width, p.height diff --git a/internal/packing/packing_test.go b/internal/packing/packing_test.go index 55a9d77d2..c233cfebf 100644 --- a/internal/packing/packing_test.go +++ b/internal/packing/packing_test.go @@ -431,3 +431,23 @@ func TestExtend3(t *testing.T) { p.Free(n1) p.Free(n2) } + +// Issue #2584 +func TestRemoveAtRootsChild(t *testing.T) { + p := packing.NewPage(32, 32, 1024) + n0 := p.Alloc(18, 18) + n1 := p.Alloc(28, 59) + n2 := p.Alloc(18, 18) + n3 := p.Alloc(18, 18) + n4 := p.Alloc(8, 10) + n5 := p.Alloc(322, 242) + _ = n5 + p.Free(n0) + p.Free(n2) + p.Free(n1) + p.Free(n3) + p.Free(n4) + + n6 := p.Alloc(18, 18) + p.Free(n6) +}