Skip to content

Commit

Permalink
lz4.go: Use unsafe.Slice to fix SliceHeader vet warnings (#17)
Browse files Browse the repository at this point in the history
Go 1.16 added vet warnings to check for unsafe uses of
reflect.SliceHeader. To fix them, use unsafe.Slice. This means this
library now requires Go 1.17, which added this function.

Fixes:

./lz4.go:162:35: possible misuse of reflect.SliceHeader
./lz4.go:509:35: possible misuse of reflect.SliceHeader
  • Loading branch information
evanj authored Dec 13, 2021
1 parent e8b7d83 commit 7f71d02
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/DataDog/golz4

go 1.13
go 1.17
16 changes: 2 additions & 14 deletions lz4.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"errors"
"fmt"
"io"
"reflect"
"unsafe"
)

Expand Down Expand Up @@ -153,13 +152,7 @@ func (w *Writer) writeFrame(src []byte) (int, error) {
}

func (w *Writer) nextInputBuffer() []byte {
w.inpBufIndex = (w.inpBufIndex + 1) % 2
tmpSlice := reflect.SliceHeader{
Data: uintptr(w.compressionBuffer[w.inpBufIndex]),
Len: streamingBlockSize,
Cap: streamingBlockSize,
}
return *(*[]byte)(unsafe.Pointer(&tmpSlice))
return unsafe.Slice((*byte)(w.compressionBuffer[w.inpBufIndex]), streamingBlockSize)
}

// Close releases all the resources occupied by Writer.
Expand Down Expand Up @@ -501,10 +494,5 @@ func (r *DecompressReader) readSize(rdr io.Reader) (int, error) {
}

func ptrToByteSlice(dataPtr unsafe.Pointer, _len, _cap int) []byte {
tmpSlice := reflect.SliceHeader{
Data: uintptr(dataPtr),
Len: _len,
Cap: _cap,
}
return *(*[]byte)(unsafe.Pointer(&tmpSlice))
return unsafe.Slice((*byte)(dataPtr), _len)
}

0 comments on commit 7f71d02

Please sign in to comment.