From b809e71d6f9a7371600e7adc8322cf77bdcb3e40 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 19 Jul 2024 11:16:51 +0100 Subject: [PATCH] lib/mmap: fix lint error on deprecated reflect.SliceHeader reflect.SliceHeader is deprecated, however the replacement gives a go vet warning so this disables the lint warning in one use of reflect.SliceHeader and replaces it in the other. --- lib/mmap/mmap_windows.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/mmap/mmap_windows.go b/lib/mmap/mmap_windows.go index f2f3c672e..55448d4a2 100644 --- a/lib/mmap/mmap_windows.go +++ b/lib/mmap/mmap_windows.go @@ -21,20 +21,29 @@ 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)) + 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 } // Free frees buffers allocated by Alloc. Note it should be passed // the same slice (not a derived slice) that Alloc returned. If the // free fails it will return with an error. func Free(mem []byte) error { - sh := (*reflect.SliceHeader)(unsafe.Pointer(&mem)) - err := windows.VirtualFree(sh.Data, 0, windows.MEM_RELEASE) + p := unsafe.SliceData(mem) + err := windows.VirtualFree(uintptr(unsafe.Pointer(p)), 0, windows.MEM_RELEASE) if err != nil { return fmt.Errorf("mmap: failed to unmap memory: %w", err) }