From 76c7481b51586cc35fd995497028e522db48dff5 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 13 Sep 2022 11:51:00 +0200 Subject: [PATCH] all: simplify unsafe.Slice usage in {Byte,UTF16}PtrToString On windows, use unsafe.Slice instead of unsafeheader as already the case for unix and plan9. The pointers are already *byte/*uint16, so the type conversion can be omitted as well. Change-Id: Ida7264cc0c1948bf563ed91d51e637edcdafb77a Reviewed-on: https://go-review.googlesource.com/c/sys/+/430515 Reviewed-by: Ian Lance Taylor Run-TryBot: Tobias Klauser TryBot-Result: Gopher Robot Reviewed-by: Bryan Mills --- plan9/syscall.go | 3 +-- unix/syscall.go | 3 +-- windows/syscall.go | 10 +--------- windows/syscall_windows.go | 8 +------- 4 files changed, 4 insertions(+), 20 deletions(-) diff --git a/plan9/syscall.go b/plan9/syscall.go index ea971285..67e5b011 100644 --- a/plan9/syscall.go +++ b/plan9/syscall.go @@ -80,8 +80,7 @@ func BytePtrToString(p *byte) string { ptr = unsafe.Pointer(uintptr(ptr) + 1) } - s := unsafe.Slice((*byte)(unsafe.Pointer(p)), n) - return string(s) + return string(unsafe.Slice(p, n)) } // Single-word zero for use when we need a valid pointer to 0 bytes. diff --git a/unix/syscall.go b/unix/syscall.go index 9916e5e8..63e8c838 100644 --- a/unix/syscall.go +++ b/unix/syscall.go @@ -80,8 +80,7 @@ func BytePtrToString(p *byte) string { ptr = unsafe.Pointer(uintptr(ptr) + 1) } - s := unsafe.Slice((*byte)(unsafe.Pointer(p)), n) - return string(s) + return string(unsafe.Slice(p, n)) } // Single-word zero for use when we need a valid pointer to 0 bytes. diff --git a/windows/syscall.go b/windows/syscall.go index 72074d58..8732cdb9 100644 --- a/windows/syscall.go +++ b/windows/syscall.go @@ -30,8 +30,6 @@ import ( "strings" "syscall" "unsafe" - - "golang.org/x/sys/internal/unsafeheader" ) // ByteSliceFromString returns a NUL-terminated slice of bytes @@ -83,13 +81,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 - - return string(s) + return string(unsafe.Slice(p, n)) } // Single-word zero for use when we need a valid pointer to 0 bytes. diff --git a/windows/syscall_windows.go b/windows/syscall_windows.go index 996cc53c..29737b20 100644 --- a/windows/syscall_windows.go +++ b/windows/syscall_windows.go @@ -138,13 +138,7 @@ func UTF16PtrToString(p *uint16) string { ptr = unsafe.Pointer(uintptr(ptr) + unsafe.Sizeof(*p)) } - var s []uint16 - h := (*unsafeheader.Slice)(unsafe.Pointer(&s)) - h.Data = unsafe.Pointer(p) - h.Len = n - h.Cap = n - - return string(utf16.Decode(s)) + return string(utf16.Decode(unsafe.Slice(p, n))) } func Getpagesize() int { return 4096 }