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 unsafe usage in Decode #1097

Merged
merged 12 commits into from
Nov 11, 2019
8 changes: 5 additions & 3 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type valuePointer struct {
Offset uint32
}

const vptrSize = unsafe.Sizeof(valuePointer{})

func (p valuePointer) Less(o valuePointer) bool {
if p.Fid != o.Fid {
return p.Fid < o.Fid
Expand All @@ -27,8 +29,6 @@ func (p valuePointer) IsZero() bool {
return p.Fid == 0 && p.Offset == 0 && p.Len == 0
}

const vptrSize = 12

// Encode encodes Pointer into byte buffer.
func (p valuePointer) Encode() []byte {
b := make([]byte, vptrSize)
Expand All @@ -39,7 +39,9 @@ func (p valuePointer) Encode() []byte {

// Decode decodes the value pointer into the provided byte buffer.
func (p *valuePointer) Decode(b []byte) {
*p = *(*valuePointer)(unsafe.Pointer(&b[0]))
// Copy over data from b into p. Using *p=unsafe.pointer(...) leads to
// pointer alignment issues. See https://github.com/dgraph-io/badger/issues/1096
copy((*[vptrSize]byte)(unsafe.Pointer(p))[:], b)
}

// header is used in value log as a header before Entry.
Expand Down
14 changes: 6 additions & 8 deletions table/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type header struct {
diff uint16 // Length of the diff.
}

const headerSize = uint16(unsafe.Sizeof(header{}))

// Encode encodes the header.
func (h header) Encode() []byte {
var b [4]byte
Expand All @@ -52,16 +54,12 @@ func (h header) Encode() []byte {
}

// Decode decodes the header.
func (h *header) Decode(buf []byte) int {
*h = *(*header)(unsafe.Pointer(&buf[0]))
return h.Size()
func (h *header) Decode(buf []byte) {
// Copy over data from buf into h. Using *h=unsafe.pointer(...) leads to
// pointer alignment issues. See https://github.com/dgraph-io/badger/issues/1096
copy((*[headerSize]byte)(unsafe.Pointer(h))[:], buf)
}

const headerSize = 4

// Size returns size of the header. Currently it's just a constant.
func (h header) Size() int { return headerSize }

// Builder is used in building a table.
type Builder struct {
// Typically tens or hundreds of meg. This is for one single file.
Expand Down
2 changes: 1 addition & 1 deletion table/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (itr *blockIterator) setIdx(i int) {
itr.key = append(itr.key[:itr.prevOverlap], itr.baseKey[itr.prevOverlap:h.overlap]...)
}
itr.prevOverlap = h.overlap
valueOff := headerSize + int(h.diff)
valueOff := headerSize + h.diff
diffKey := entryData[headerSize:valueOff]
itr.key = append(itr.key[:h.overlap], diffKey...)
itr.val = entryData[valueOff:]
Expand Down