Skip to content

Commit 4fe4601

Browse files
cuiweixiegopherbot
authored andcommitted
runtime: simplify code using unsafe.{Slice,String}
Updates #54854 Change-Id: Ie18665e93e477b6f220acf4c6c070b2af4343064 Reviewed-on: https://go-review.googlesource.com/c/go/+/428157 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
1 parent 2ee075d commit 4fe4601

File tree

3 files changed

+3
-12
lines changed

3 files changed

+3
-12
lines changed

src/runtime/debuglog.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,7 @@ func (r *debugLogReader) printVal() bool {
657657
case debugLogConstString:
658658
len, ptr := int(r.uvarint()), uintptr(r.uvarint())
659659
ptr += firstmoduledata.etext
660-
str := stringStruct{
661-
str: unsafe.Pointer(ptr),
662-
len: len,
663-
}
664-
s := *(*string)(unsafe.Pointer(&str))
660+
s := unsafe.String((*byte)(unsafe.Pointer(ptr)), len)
665661
print(s)
666662

667663
case debugLogStringOverflow:

src/runtime/memmove_linux_amd64_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package runtime_test
66

77
import (
88
"os"
9-
"reflect"
109
"syscall"
1110
"testing"
1211
"unsafe"
@@ -45,11 +44,7 @@ func TestMemmoveOverflow(t *testing.T) {
4544
defer syscall.Syscall(syscall.SYS_MUNMAP, base+off, 65536, 0)
4645
}
4746

48-
var s []byte
49-
sp := (*reflect.SliceHeader)(unsafe.Pointer(&s))
50-
sp.Data = base
51-
sp.Len, sp.Cap = 3<<30, 3<<30
52-
47+
s := unsafe.Slice((*byte)(unsafe.Pointer(base)), 3<<30)
5348
n := copy(s[1:], s)
5449
if n != 3<<30-1 {
5550
t.Fatalf("copied %d bytes, expected %d", n, 3<<30-1)

src/runtime/proc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ func cpuinit() {
620620

621621
for i := int32(0); i < n; i++ {
622622
p := argv_index(argv, argc+1+i)
623-
s := *(*string)(unsafe.Pointer(&stringStruct{unsafe.Pointer(p), findnull(p)}))
623+
s := unsafe.String(p, findnull(p))
624624

625625
if hasPrefix(s, prefix) {
626626
env = gostring(p)[len(prefix):]

0 commit comments

Comments
 (0)