Skip to content

cmd/compile: unsafe.Add bug when adding uint8 value to a pointer #48536

@rip-create-your-account

Description

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

go version go1.17.1 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/anon/.cache/go-build"
GOENV="/home/anon/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/anon/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/anon/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.1"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build726424511=/tmp/go-build -gno-record-gcc-switches"

What did you do?

See https://play.golang.org/p/KR_dZZU0BDN. I used unsafe.Add in a loop to randomly advance a unsafe.Pointer that points to a byte array. The len argument type is uint8 where value is randomly 0 or 1. Also there's a function that would manipulate the value behind the pointer but it's not used.

What did you expect to see?

I expected the program to exit cleanly as the unsafe operations seem valid.

What did you see instead?

SIGSEGV crash for line 22 on the second iteration of the loop because the first dstNext = unsafe.Add(dstNext, uint8(adv)) seems to store a garbage value to dstNext. When len is of any other integer type the program exits cleanly. Try changing the line 23 to dstNext = unsafe.Add(dstNext, uint16(adv)). Also removing the unused function makes the program exit cleanly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions