You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First I had var i int, and assigned it to an empty interface var e interface{} = i.
Then I tried to modify the value which e.data point to. Unexpectedly, i was changed too. i is a variable, but not pointer, so I think it's value should NOT be changed.
Just run the code below, see the output.
package main
import (
"fmt""unsafe"
)
funcmain() {
issue(101, 233) // both < 256issue(253, 886) // oldVal < 256issue(987, 254) // newVal < 256issue(998, 258) // both >= 256// Try this four calls, the output will be more strange.// issue(100, 255) // both < 256// issue(255, 999) // oldVal < 256// issue(999, 255) // newVal < 256// issue(999, 256) // both >= 256
}
// type eface struct {// _type *_type// data unsafe.Pointer// }// data is *int (unsafe.Pointer(&i))funcissue(oldVal, newValint) {
fmt.Printf("\n==== running issue(%d, %d) ====\n", oldVal, newVal)
var (
i=oldVal// inteinterface{} =i
)
pdata:=unsafe.Pointer(uintptr(unsafe.Pointer(&e)) +unsafe.Sizeof(&i))
data:=unsafe.Pointer(*(**int)(pdata))
fmt.Printf(" i, &i = %d, %p\n", i, &i)
fmt.Printf(" *data, data = %d, %p\n", *((*int)(data)), data)
// when 0 <= oldVal < 256, i will be changed, WHY???*((*int)(data)) =newValfmt.Printf("\n exec *((*int)(data)) = %d\n\n", newVal)
fmt.Printf(" i, &i = %d, %p\n", i, &i)
fmt.Printf(" *data, data = %d, %p\n", *((*int)(data)), data)
}
Maybe it's a feature/bug about small integer cache...?
The text was updated successfully, but these errors were encountered:
You are not allowed to write to the location pointed to by the data field of an interface. Interfaces are assumed to be immutable.
(Just like you can't write to the backing store of a string, or the type pointer of an interface, or lots of other things.)
You are not allowed to write to the location pointed to by the data field of an interface. Interfaces are assumed to be immutable.
(Just like you can't write to the backing store of a string, or the type pointer of an interface, or lots of other things.)
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes, the output of the codes below on my machine is the same as golang playground (1.15.5)
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
First I had
var i int
, and assigned it to an empty interfacevar e interface{} = i
.Then I tried to modify the value which
e.data
point to. Unexpectedly,i
was changed too.i
is a variable, but not pointer, so I think it's value should NOT be changed.Just run the code below, see the output.
Maybe it's a feature/bug about small integer cache...?
The text was updated successfully, but these errors were encountered: