Skip to content

cmd/compile: incorrect assignment to uint64 via pointer converted to *uint16 (new in 1.7) #16769

@kelseyfrancis

Description

@kelseyfrancis

What version of Go are you using (go version)?

go version go1.7 darwin/amd64 and go version go1.7 linux/amd64.

This issue is not present in go1.6.2.

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"

and

GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"

What did you do?

Assign a uint16 (or uint32) to a previously unused uint64 via converted pointer on a little-endian architecture. go run the following program:

package main

import (
    "fmt"
    "unsafe"
)

func main() {
    var x uint64
    *(*uint16)(unsafe.Pointer(&x)) = 0xffee
    fmt.Printf("%x\n", x)

    var y uint64
    fmt.Printf("%x\n", y)
    *(*uint16)(unsafe.Pointer(&y)) = 0xffee
    fmt.Printf("%x\n", y)
}

What did you expect to see?

ffee
0
ffee

What did you see instead?

c42000ffee
0
ffee

A test that passes with Go 1.6 but not with Go 1.7:

// +build amd64

package main

import (
    "fmt"
    "unsafe"
)

func ExampleUint16NoAccessBeforeAssignment() {
    var x uint64
    *(*uint16)(unsafe.Pointer(&x)) = 0xffee
    fmt.Printf("%x\n", x)
    // Output:
    // ffee
}

func ExampleUint16AccessBeforeAssignment() {
    var x uint64
    fmt.Printf("%x\n", x)
    *(*uint16)(unsafe.Pointer(&x)) = 0xffee
    fmt.Printf("%x\n", x)
    // Output:
    // 0
    // ffee
}

func ExampleUint32NoAccessBeforeAssignment() {
    var x uint64
    *(*uint32)(unsafe.Pointer(&x)) = 0xffeeddcc
    fmt.Printf("%x\n", x)
    // Output:
    // ffeeddcc
}

func ExampleUint32AccessBeforeAssignment() {
    var x uint64
    fmt.Printf("%x\n", x)
    *(*uint32)(unsafe.Pointer(&x)) = 0xffeeddcc
    fmt.Printf("%x\n", x)
    // Output:
    // 0
    // ffeeddcc
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions