Skip to content
This repository was archived by the owner on Mar 9, 2019. It is now read-only.

Commit 583e893

Browse files
authored
Merge pull request #584 from benbjohnson/go17
Fix Go 1.7 pointer reference bug
2 parents 94c8db5 + 92410e0 commit 583e893

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

freelist.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,16 @@ func (f *freelist) read(p *page) {
166166
}
167167

168168
// Copy the list of page ids from the freelist.
169-
ids := ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[idx:count]
170-
f.ids = make([]pgid, len(ids))
171-
copy(f.ids, ids)
169+
if count == 0 {
170+
f.ids = nil
171+
} else {
172+
ids := ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[idx:count]
173+
f.ids = make([]pgid, len(ids))
174+
copy(f.ids, ids)
172175

173-
// Make sure they're sorted.
174-
sort.Sort(pgids(f.ids))
176+
// Make sure they're sorted.
177+
sort.Sort(pgids(f.ids))
178+
}
175179

176180
// Rebuild the page cache.
177181
f.reindex()
@@ -189,7 +193,9 @@ func (f *freelist) write(p *page) error {
189193

190194
// The page.count can only hold up to 64k elements so if we overflow that
191195
// number then we handle it by putting the size in the first element.
192-
if len(ids) < 0xFFFF {
196+
if len(ids) == 0 {
197+
p.count = uint16(len(ids))
198+
} else if len(ids) < 0xFFFF {
193199
p.count = uint16(len(ids))
194200
copy(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[:], ids)
195201
} else {

node.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ func (n *node) write(p *page) {
201201
}
202202
p.count = uint16(len(n.inodes))
203203

204+
// Stop here if there are no items to write.
205+
if p.count == 0 {
206+
return
207+
}
208+
204209
// Loop over each item and write it to the page.
205210
b := (*[maxAllocSize]byte)(unsafe.Pointer(&p.ptr))[n.pageElementSize()*len(n.inodes):]
206211
for i, item := range n.inodes {

page.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func (p *page) leafPageElement(index uint16) *leafPageElement {
6262

6363
// leafPageElements retrieves a list of leaf nodes.
6464
func (p *page) leafPageElements() []leafPageElement {
65+
if p.count == 0 {
66+
return nil
67+
}
6568
return ((*[0x7FFFFFF]leafPageElement)(unsafe.Pointer(&p.ptr)))[:]
6669
}
6770

@@ -72,6 +75,9 @@ func (p *page) branchPageElement(index uint16) *branchPageElement {
7275

7376
// branchPageElements retrieves a list of branch nodes.
7477
func (p *page) branchPageElements() []branchPageElement {
78+
if p.count == 0 {
79+
return nil
80+
}
7581
return ((*[0x7FFFFFF]branchPageElement)(unsafe.Pointer(&p.ptr)))[:]
7682
}
7783

0 commit comments

Comments
 (0)