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

Commit

Permalink
fix Go 1.7 pointer reference bug
Browse files Browse the repository at this point in the history
This commit fixes a bug where page end-of-header pointers were being
converted to byte slices even when the pointer did not point to
allocated memory. This occurs with pages that have a `page.count`
of zero.

Note: This was not an issue in Go 1.6 but the new Go 1.7 SSA backend
handles `nil` checks differently.

See golang/go#16772
  • Loading branch information
benbjohnson committed Aug 18, 2016
1 parent 94c8db5 commit 92410e0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
18 changes: 12 additions & 6 deletions freelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,16 @@ func (f *freelist) read(p *page) {
}

// Copy the list of page ids from the freelist.
ids := ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[idx:count]
f.ids = make([]pgid, len(ids))
copy(f.ids, ids)
if count == 0 {
f.ids = nil
} else {
ids := ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[idx:count]
f.ids = make([]pgid, len(ids))
copy(f.ids, ids)

// Make sure they're sorted.
sort.Sort(pgids(f.ids))
// Make sure they're sorted.
sort.Sort(pgids(f.ids))
}

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

// The page.count can only hold up to 64k elements so if we overflow that
// number then we handle it by putting the size in the first element.
if len(ids) < 0xFFFF {
if len(ids) == 0 {
p.count = uint16(len(ids))
} else if len(ids) < 0xFFFF {
p.count = uint16(len(ids))
copy(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[:], ids)
} else {
Expand Down
5 changes: 5 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ func (n *node) write(p *page) {
}
p.count = uint16(len(n.inodes))

// Stop here if there are no items to write.
if p.count == 0 {
return
}

// Loop over each item and write it to the page.
b := (*[maxAllocSize]byte)(unsafe.Pointer(&p.ptr))[n.pageElementSize()*len(n.inodes):]
for i, item := range n.inodes {
Expand Down
6 changes: 6 additions & 0 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func (p *page) leafPageElement(index uint16) *leafPageElement {

// leafPageElements retrieves a list of leaf nodes.
func (p *page) leafPageElements() []leafPageElement {
if p.count == 0 {
return nil
}
return ((*[0x7FFFFFF]leafPageElement)(unsafe.Pointer(&p.ptr)))[:]
}

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

// branchPageElements retrieves a list of branch nodes.
func (p *page) branchPageElements() []branchPageElement {
if p.count == 0 {
return nil
}
return ((*[0x7FFFFFF]branchPageElement)(unsafe.Pointer(&p.ptr)))[:]
}

Expand Down

0 comments on commit 92410e0

Please sign in to comment.