-
Notifications
You must be signed in to change notification settings - Fork 18k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/compile: encoding/binary.PutUint16 sometimes doesn't write #59367
Comments
Shorter reproducer: func main() {
flag := new(bool)
*flag = true
var buf1, buf2 []byte
buf2 = []byte{3} // This is somehow important. What is printed is still [0 0].
var x uint16
if *flag {
x = 1
}
buf1 = []byte{byte(x >> 8), byte(x)}
buf2 = []byte{byte(x >> 8), byte(x)}
fmt.Println(buf1)
fmt.Println(buf2)
} |
cc @golang/compiler |
Doesn't reproduce with setting GOAMD64=v3. Guess because MOVBEW instruction is available, so avoiding the problem. |
Probably. Doesn't reproduce on 386, arm or arm64, but it does on amd64 w/ Go 1.12 (!). |
Looks like the 2-byte byte swap instruction we use here is not typed correctly. It gets a bool type instead of a uint16 type. So if it is spilled/restored, it loses its high 8 bits. |
Change https://go.dev/cl/481395 mentions this issue: |
@gopherbot please open backport issues for 1.20 and 1.19. |
Backport issue(s) opened: #59373 (for 1.19), #59374 (for 1.20). Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://go.dev/wiki/MinorReleases. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes, also reproduced with go 1.18 and 1.19
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
In certain situations binary.BigEndian.PutUint16 does not modify the targeted byte slice correctly.
See https://go.dev/play/p/OjvbLQZvYcn.
Note that the code runs correctly when a dlv debugger is attached to the program. The behaviour is the same if PutUint32 is used instead of PutUint16. Or if the shifted bytes are assigned directly like this:
What did you expect to see?
PutUint16 updates two buffers with the same value under given circumstances.
What did you see instead?
bufferTwo did not get updated or updated with wrong value.
The text was updated successfully, but these errors were encountered: