Re: [go-nuts] Re: Is the following funciton always safe to convert a string to a slice?

2020-04-24 Thread T L
Many thanks. This is quite informative. On Wednesday, April 22, 2020 at 4:44:18 PM UTC-4, Ian Lance Taylor wrote: > > On Wed, Apr 22, 2020 at 7:08 AM T L > > wrote: > > > > I understand that, by the case 6 in the unsafe package docs, > > the second program should be safe with or without the ru

Re: [go-nuts] Re: Is the following funciton always safe to convert a string to a slice?

2020-04-22 Thread Robert Engels
But it’s a data race regardless. Even if the runtime did interrupt the go routine mid write and change the stack it would be forced to eventually correct the value to have the write be valid - but if another routine is reading that value, including the GC, there must be synchronization added. In

Re: [go-nuts] Re: Is the following funciton always safe to convert a string to a slice?

2020-04-22 Thread Ian Lance Taylor
On Wed, Apr 22, 2020 at 7:08 AM T L wrote: > > I understand that, by the case 6 in the unsafe package docs, > the second program should be safe with or without the runtime.KeepAlive call. > I just wonder how Go runtime achieves this goal. > I have an impression that pointer assignments in Go by gc

Re: [go-nuts] Re: Is the following funciton always safe to convert a string to a slice?

2020-04-22 Thread Ian Lance Taylor
On Wed, Apr 22, 2020 at 4:33 AM T L wrote: > > On Tuesday, April 21, 2020 at 8:13:17 PM UTC-4, Ian Lance Taylor wrote: >> >> On Tue, Apr 21, 2020 at 8:17 AM T L wrote: >> > >> > And is the runtime.KeepAlive call in the following program essential to >> > keep the correctness? >> > >> > package m

Re: [go-nuts] Re: Is the following funciton always safe to convert a string to a slice?

2020-04-22 Thread T L
I understand that, by the case 6 in the unsafe package docs, the second program should be safe with or without the runtime.KeepAlive call. I just wonder how Go runtime achieves this goal. I have an impression that pointer assignments in Go by gc is atomically, but is this also true for uintptr val

Re: [go-nuts] Re: Is the following funciton always safe to convert a string to a slice?

2020-04-22 Thread T L
On Tuesday, April 21, 2020 at 8:13:17 PM UTC-4, Ian Lance Taylor wrote: > > On Tue, Apr 21, 2020 at 8:17 AM T L > > wrote: > > > > And is the runtime.KeepAlive call in the following program essential to > keep the correctness? > > > > package main > > > > import ( > > "fmt" > >

Re: [go-nuts] Re: Is the following funciton always safe to convert a string to a slice?

2020-04-21 Thread Ian Lance Taylor
On Tue, Apr 21, 2020 at 8:17 AM T L wrote: > > And is the runtime.KeepAlive call in the following program essential to keep > the correctness? > > package main > > import ( > "fmt" > "unsafe" > "reflect" > "runtime" > ) > > func main() { > a := [6]byte{'G', 'o', 'o', 'g', 'l',

[go-nuts] Re: Is the following funciton always safe to convert a string to a slice?

2020-04-21 Thread T L
And is the runtime.KeepAlive call in the following program essential to keep the correctness? package main import ( "fmt" "unsafe" "reflect" "runtime" ) func main() { a := [6]byte{'G', 'o', 'o', 'g', 'l', 'e'} bs := []byte("Golang") hdr := (*reflect.SliceHeader)(unsa