Skip to content
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/vet: incorrectly flags an array as too small when initialized with a constant assigned from unsafe.Sizeof literal #60734

Closed
jonbodner opened this issue Jun 12, 2023 · 7 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@jonbodner
Copy link

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

$ go version
go version go1.20.5 darwin/arm64

(also shows up on Go playground)

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="arm64"
GOBIN=""
GOCACHE="/Users/jon/Library/Caches/go-build"
GOENV="/Users/jon/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/jon/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/jon/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.20.5"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/jon/projects/vet_bug/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/hj/mr53lrfj1bqf4hg62wqkp4000000gn/T/go-build3455060576=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"fmt"
	"unsafe"
)

type Data struct {
	Value  uint32   // 4 bytes
	Label  [10]byte // 10 bytes
	Active bool     // 1 byte
	// padded with 1 byte to make it align
}

func (d Data) String() string {
	return fmt.Sprintf(`{Value:%d, Label:%s, Active: %v}`, d.Value, string(d.Label[:]), d.Active)
}

const (
	dataSize = unsafe.Sizeof(Data{})
	dataSizeLiteral = 16
)

func main() {
	fmt.Println(dataSize)
	// use a literal value for array size: no go vet error
	incomingData2 := [16]byte{0, 132, 95, 237, 80, 104, 111, 110, 101, 0, 0, 0, 0, 0, 1, 0}
	fmt.Println(incomingData2)

	// use a constant based on unsafe.Sizeof for array size: ERROR INCORRECTLY REPORTED BY GO VET
	incomingData := [dataSize]byte{0, 132, 95, 237, 80, 104, 111, 110, 101, 0, 0, 0, 0, 0, 1, 0}
	fmt.Println(incomingData)

	// double check that the values aren't different
	fmt.Println(incomingData == incomingData2)
	fmt.Println(dataSize == 16)

	// constant has literal value: no go vet error
        incomingData3 := [dataSizeLiteral]byte{0, 132, 95, 237, 80, 104, 111, 110, 101, 0, 0, 0, 0, 0, 1, 0}
        fmt.Println(incomingData3)
}

run go vet on this code.

(also can find it at https://go.dev/play/p/q6r-qRpAGFa )

What did you expect to see?

I expected no go vet warning.

What did you see instead?

I get the following message from go vet:

$ go vet main.go
# command-line-arguments
vet: ./main.go:31:92: index 15 is out of bounds (>= 15)
@ianlancetaylor
Copy link
Contributor

Related to #60431.

@seankhliao seankhliao changed the title go vet: incorrectly flags an array as too small when initialized with a constant assigned from unsafe.Sizeof literal cmd/vet: incorrectly flags an array as too small when initialized with a constant assigned from unsafe.Sizeof literal Jun 12, 2023
@bcmills bcmills added the Tools This label describes issues relating to any tools in the x/tools repository. label Jun 12, 2023
@prattmic prattmic added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 13, 2023
@prattmic prattmic added this to the Backlog milestone Jun 13, 2023
@prattmic
Copy link
Member

cc @matloob @timothy-king

@timothy-king
Copy link
Contributor

This looks like it a type-checking failure and a likely duplicate of #60734 . Let's come back to this after the other issue has been addressed.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/501495 mentions this issue: go/analysis: add Sizes that matches gc size computations

@cuonglm
Copy link
Member

cuonglm commented Jun 16, 2023

This looks like it a type-checking failure and a likely duplicate of #60734 . Let's come back to this after the other issue has been addressed.

It's duplicated, I'm leaning to close this one in favor of #60431

gopherbot pushed a commit to golang/tools that referenced this issue Jun 27, 2023
For golang/go#60431
For golang/go#60734

Change-Id: I6a15a24e3e121635b33d77fde9170a41514c0db1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/501495
Run-TryBot: Cuong Manh Le <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
Auto-Submit: Cuong Manh Le <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Robert Griesemer <[email protected]>
Auto-Submit: Robert Griesemer <[email protected]>
gopls-CI: kokoro <[email protected]>
Reviewed-by: Robert Griesemer <[email protected]>
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/506715 mentions this issue: go/types, types2: add Sizes that match actual gc behavior

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/506856 mentions this issue: go/types, types2: add Sizes that match actual gc behavior

@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jun 28, 2023
@dmitshur dmitshur modified the milestones: Backlog, Go1.22 Jun 28, 2023
@golang golang locked and limited conversation to collaborators Aug 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

8 participants