Skip to content

cmd/compile: missed bounds check elimination when paired slices are resliced #70062

Open
@FiloSottile

Description

@FiloSottile
go version devel go1.24-140308837f Mon Oct 21 15:30:47 2024 +0200 darwin/arm64

When the lengths of two slices are "paired" (dst = dst[:len(src)]), the compiler is smart enough to apply ifs to both, but if you reslice them the pairing is lost.

func foo(src, dst []byte) {
	dst = dst[:len(src)]
	for len(src) >= 50 {
		_ = (*[50]byte)(src) // no bounds check
		_ = (*[50]byte)(dst) // no bounds check
	}
}

func bar(src, dst []byte) {
	dst = dst[:len(src)]
	for len(src) >= 50 {
		src = src[50:] // no bounds check
		dst = dst[50:] // bounds check!
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions