Skip to content

proposal: x/tools/go/analysis/passes/appends: check for incorrect slice length initialization  #69872

Open
@cuishuang

Description

@cuishuang

Proposal Details

The following code exists in many projects, and developers actually want [0 1 2], but due to the initialization error of slice, the final result is [0 0 0 0 1 2]

package main

import "fmt"

func main() {

	sli := make([]int, 3)

	for i := range 3 {
		sli = append(sli, i)
	}

	fmt.Println(sli) // the result is [0 0 0 0 1 2]

}

The online demo: https://go.dev/play/p/q1BcVCmvidW

Over the past few months, I have conducted extensive research and analysis, and also submitted pull requests to fix issues in many well-known Go projects such as prometheus, zap, vitess. Below are some pull requests submitted by me and others related to this problem.

Due to limitations in search skills and time, I only checked records of such issues in the past few months. The history of more such issues has not been traced back. But I think it's already enough

I would like to propose adding a new analyzer to go vet that can detect such situations, thereby avoiding these issues in the future.

Now I have already completed an initial version of the code, and if the proposal is approved, I would be happy to refine it and add the necessary test cases.

The merged pr:

prometheus/prometheus#14702 (comment)

uber-go/zap#1461

cadence-workflow/cadence#6293

prometheus/prometheus#15026

vitessio/vitess#16674

kedacore/keda#6179

external-secrets/external-secrets#3964

brianvoe/gofakeit#365

fission/fission#3018

DataDog/datadog-agent#29744

superseriousbusiness/gotosocial#3382

ccfos/nightingale#2169

gookit/color#97

vdaas/vald#2672

supabase/auth#1788

pufferpanel/pufferpanel#1367

juju/juju#18176

go-spatial/tegola@0f3131f

lxc/incus#1285

yunionio/cloudpods#21346

taubyte/tau#253

fleetdm/fleet#22608

antrea-io/antrea#6715

tdewolff/canvas#315

Consensys/gnark#1288

superfly/flyctl#3982

bazel-contrib/rules_go#4133

zitadel/oidc#658

jhump/protoreflect#629

apache/rocketmq-client-go#1171

edgexfoundry/edgex-go#4938

dolthub/doltgresql#812

apache/trafficcontrol#8091

pingcap/tidb-operator#5755

botlabs-gg/yagpdb#1734

Altinity/clickhouse-backup#1019

openshift/installer#9072

GoogleCloudPlatform/magic-modules#11919

openmeterio/openmeter#1615

target/goalert#4090

kubeovn/kube-ovn#4579

syyongx/php2go#49

fluid-cloudnative/fluid#4335

akuity/kargo#2648

kubernetes/kubernetes#127785

apache/dubbo-go#2734

letsencrypt/boulder#7725

cortexproject/cortex#6237

kubeedge/kubeedge#5895

grafana/mimir#9449

rocboss/paopao-ce#581

authelia/authelia#7720

cilium/cilium#35164

git-lfs/git-lfs#5874

https://github.com/hashicorp/nomad/pull/24109/files

cosmos/ibc-go#6444

minio/minio#19567

VictoriaMetrics/VictoriaMetrics#6897

hyperledger/fabric#4956

grafana/pyroscope#3600

cosmos/cosmos-sdk#21494 (review)

anchore/grype#2133

https://github.com/ethereum-optimism/optimism/pull/11542/files

https://github.com/libp2p/go-libp2p/pull/2938/files

stashapp/stash#5327

trufflesecurity/trufflehog#3293

c9s/bbgo#1724 (comment)

cosmos/cosmos-sdk#22006

FerretDB/FerretDB#4598

dagger/dagger#8612

letsencrypt/boulder#7731

Layr-Labs/eigenda#767

wal-g/wal-g#1800

VictoriaMetrics/VictoriaMetrics#7161

harmony-one/harmony#4767

stackrox/stackrox#13028

stefanprodan/timoni#430

Altinity/clickhouse-operator#1523

iotexproject/iotex-core#4412

ane more in review process.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    • Status

      Hold

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions