Open FiloSottile opened 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.
dst = dst[:len(src)]
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! } }
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
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.