What's your platform? On M1, checkptr reports an error for me. The code:
ptr := unsafe.Pointer(rf.UnsafeAddr()) is safe, but: x := rf.UnsafeAddr() ptr = unsafe.Pointer(x) is not. Cuong Manh Le https://cuonglm.xyz On Sun, Apr 18, 2021 at 6:46 AM Name No <zylthink...@gmail.com> wrote: > > reflect.Value.UnsafeAddr has the commence attached like that: > > //go:nocheckptr > // This prevents inlining Value.UnsafeAddr when -d=checkptr is enabled, > // which ensures cmd/compile can recognize unsafe.Pointer(v.UnsafeAddr()) > // and make an exception. > > Seems it is saying unsafe.Pointer(reflect.Value.UnsafeAddr()) is > dangerous that go:nocheckptr can disable UnsafeAddr from inline, so > cmd/compile can detect it is a function call and raise an exception. > > But indeed, the following code will not raise any exception at ptr := > unsafe.Pointer(rf.UnsafeAddr()). > It indicates that unsafe.Pointer(rf.UnsafeAddr()) is safe. > > Anybody can help to explain this? > > go run -race -gcflags=all=-d=checkptr c.go > package main > > import ( > "reflect" > "unsafe" > "fmt" > ) > > func main() { > var s = struct{ foo int }{100} > > rs := reflect.ValueOf(&s).Elem() > rf := rs.Field(0) > > ptr := unsafe.Pointer(rf.UnsafeAddr()) > > x := rf.UnsafeAddr() > ptr = unsafe.Pointer(x) > fmt.Println(ptr) > } > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/14da5c55-9137-46bc-90e1-b51b8ef6553bn%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/14da5c55-9137-46bc-90e1-b51b8ef6553bn%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CALS7p_eYX2_n0QFKJQFX9xVopRAKoMggQCZxhoD9AjeKrFEMXw%40mail.gmail.com.