From aba9fc2a8ff2c9439446386f616b860442f0cf9a Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 6 Sep 2022 10:59:06 +0200 Subject: [PATCH] unix: use unsafe.Slice instead of unsafeheader package Go 1.18 is the minimum supported Go version and unsafe.Slice was introduced in Go 1.17. Change-Id: Ie5f1fad01f219e7b7a190de2c49676f366ad3bc7 Reviewed-on: https://go-review.googlesource.com/c/sys/+/428515 TryBot-Result: Gopher Robot Reviewed-by: Benny Siegert Reviewed-by: Ian Lance Taylor Run-TryBot: Tobias Klauser --- unix/syscall.go | 9 +-------- unix/syscall_darwin.1_13.go | 12 ++---------- unix/syscall_unix.go | 8 +------- unix/sysvshm_unix.go | 13 ++----------- 4 files changed, 6 insertions(+), 36 deletions(-) diff --git a/unix/syscall.go b/unix/syscall.go index 649fa874..9916e5e8 100644 --- a/unix/syscall.go +++ b/unix/syscall.go @@ -29,8 +29,6 @@ import ( "bytes" "strings" "unsafe" - - "golang.org/x/sys/internal/unsafeheader" ) // ByteSliceFromString returns a NUL-terminated slice of bytes @@ -82,12 +80,7 @@ func BytePtrToString(p *byte) string { ptr = unsafe.Pointer(uintptr(ptr) + 1) } - var s []byte - h := (*unsafeheader.Slice)(unsafe.Pointer(&s)) - h.Data = unsafe.Pointer(p) - h.Len = n - h.Cap = n - + s := unsafe.Slice((*byte)(unsafe.Pointer(p)), n) return string(s) } diff --git a/unix/syscall_darwin.1_13.go b/unix/syscall_darwin.1_13.go index 1596426b..1259f6df 100644 --- a/unix/syscall_darwin.1_13.go +++ b/unix/syscall_darwin.1_13.go @@ -7,11 +7,7 @@ package unix -import ( - "unsafe" - - "golang.org/x/sys/internal/unsafeheader" -) +import "unsafe" //sys closedir(dir uintptr) (err error) //sys readdir_r(dir uintptr, entry *Dirent, result **Dirent) (res Errno) @@ -86,11 +82,7 @@ func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) { } // Copy entry into return buffer. - var s []byte - hdr := (*unsafeheader.Slice)(unsafe.Pointer(&s)) - hdr.Data = unsafe.Pointer(&entry) - hdr.Cap = reclen - hdr.Len = reclen + s := unsafe.Slice((*byte)(unsafe.Pointer(&entry)), reclen) copy(buf, s) buf = buf[reclen:] diff --git a/unix/syscall_unix.go b/unix/syscall_unix.go index 1ff5060b..9f753560 100644 --- a/unix/syscall_unix.go +++ b/unix/syscall_unix.go @@ -13,8 +13,6 @@ import ( "sync" "syscall" "unsafe" - - "golang.org/x/sys/internal/unsafeheader" ) var ( @@ -117,11 +115,7 @@ func (m *mmapper) Mmap(fd int, offset int64, length int, prot int, flags int) (d } // Use unsafe to convert addr into a []byte. - var b []byte - hdr := (*unsafeheader.Slice)(unsafe.Pointer(&b)) - hdr.Data = unsafe.Pointer(addr) - hdr.Cap = length - hdr.Len = length + b := unsafe.Slice((*byte)(unsafe.Pointer(addr)), length) // Register mapping in m and return it. p := &b[cap(b)-1] diff --git a/unix/sysvshm_unix.go b/unix/sysvshm_unix.go index 0bb4c8de..5bb41d17 100644 --- a/unix/sysvshm_unix.go +++ b/unix/sysvshm_unix.go @@ -7,11 +7,7 @@ package unix -import ( - "unsafe" - - "golang.org/x/sys/internal/unsafeheader" -) +import "unsafe" // SysvShmAttach attaches the Sysv shared memory segment associated with the // shared memory identifier id. @@ -34,12 +30,7 @@ func SysvShmAttach(id int, addr uintptr, flag int) ([]byte, error) { } // Use unsafe to convert addr into a []byte. - // TODO: convert to unsafe.Slice once we can assume Go 1.17 - var b []byte - hdr := (*unsafeheader.Slice)(unsafe.Pointer(&b)) - hdr.Data = unsafe.Pointer(addr) - hdr.Cap = int(info.Segsz) - hdr.Len = int(info.Segsz) + b := unsafe.Slice((*byte)(unsafe.Pointer(addr)), int(info.Segsz)) return b, nil }