cmd/compile: add missing bound checks when handle zero-sized values

Fixes #79197

Change-Id: Ifded6ec4c014d2e87dab7b66c8063f8ffa66be5d
Reviewed-on: https://go-review.googlesource.com/c/go/+/774120
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
Cuong Manh Le 2026-05-05 15:21:47 +07:00 committed by Gopher Robot
parent e929fb78e4
commit 6f19c3b459
2 changed files with 43 additions and 0 deletions

View file

@ -4549,6 +4549,8 @@ func (s *state) assignWhichMayOverlap(left ir.Node, right *ssa.Value, deref bool
return
}
if t.Size() == 0 {
len := s.constInt(types.Types[types.TINT], n)
s.boundsCheck(i, len, ssa.BoundsIndex, false)
return
}

View file

@ -0,0 +1,41 @@
// run
// Copyright 2026 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
//go:noinline
func a1(i int) {
var a [1]struct{}
a[i] = struct{}{}
}
//go:noinline
func a2(i int) {
var a [1][0]int
a[i] = [0]int{}
}
//go:noinline
func a3(i int) {
var a [1]struct{ x [0]int }
a[i] = struct{ x [0]int }{}
}
func wantPanic(name string, f func()) {
defer func() {
if r := recover(); r != nil {
return
}
panic(name + ": no panic (bug)")
}()
f()
}
func main() {
wantPanic("a1", func() { a1(5) })
wantPanic("a2", func() { a2(5) })
wantPanic("a3", func() { a3(5) })
}