golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.73k stars 17.63k forks source link

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

Open FiloSottile opened 2 hours ago

FiloSottile commented 2 hours ago
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!
    }
}
gabyhelp commented 2 hours ago

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)