mirror of
https://github.com/rclone/rclone.git
synced 2025-12-11 22:14:05 +01:00
lib/mmap: convert to using unsafe.Slice to avoid deprecated reflect.SliceHeader
This commit is contained in:
@@ -7,7 +7,6 @@ package mmap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
@@ -21,21 +20,10 @@ func Alloc(size int) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mmap: failed to allocate memory for buffer: %w", err)
|
||||
}
|
||||
// SliceHeader is deprecated...
|
||||
var mem []byte
|
||||
sh := (*reflect.SliceHeader)(unsafe.Pointer(&mem)) // nolint:staticcheck
|
||||
sh.Data = p
|
||||
sh.Len = size
|
||||
sh.Cap = size
|
||||
return mem, nil
|
||||
// ...However the correct code gives a go vet warning
|
||||
// "possible misuse of unsafe.Pointer"
|
||||
//
|
||||
// Maybe there is a different way of writing this, but none of
|
||||
// the allowed uses of unsafe.Pointer seemed to cover it other
|
||||
// than using SliceHeader (use 6 of unsafe.Pointer).
|
||||
//
|
||||
// return unsafe.Slice((*byte)(unsafe.Pointer(p)), size), nil
|
||||
|
||||
pp := unsafe.Pointer(&p)
|
||||
up := *(*unsafe.Pointer)(pp)
|
||||
return unsafe.Slice((*byte)(up), size), nil
|
||||
}
|
||||
|
||||
// Free frees buffers allocated by Alloc. Note it should be passed
|
||||
|
||||
Reference in New Issue
Block a user