diff --git a/src/cmd/compile/internal/ssagen/ssa.go b/src/cmd/compile/internal/ssagen/ssa.go index a86c940356..864f1708f8 100644 --- a/src/cmd/compile/internal/ssagen/ssa.go +++ b/src/cmd/compile/internal/ssagen/ssa.go @@ -3555,9 +3555,9 @@ func (s *state) exprCheckPtr(n ir.Node, checkPtrOK bool) *ssa.Value { // use constants for the bounds check. z := s.constInt(types.Types[types.TINT], 0) s.boundsCheck(z, z, ssa.BoundsIndex, false) - // The return value won't be live, return junk. - // But not quite junk, in case bounds checks are turned off. See issue 48092. - return s.zeroVal(n.Type()) + // The return value won't be live. In case bounds checks + // are turned off, load from (*T)(nil) to cause a segfault. + return s.load(n.Type(), s.constNil(n.Type().PtrTo())) } len := s.constInt(types.Types[types.TINT], bound) s.boundsCheck(i, len, ssa.BoundsIndex, n.Bounded()) // checks i == 0 diff --git a/test/fixedbugs/issue77868.go b/test/fixedbugs/issue77868.go new file mode 100644 index 0000000000..ccaeef549e --- /dev/null +++ b/test/fixedbugs/issue77868.go @@ -0,0 +1,20 @@ +// compile + +// 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 p + +type S struct { + n int + a [2]int +} + +func f(i int) int { + var arr [0]S + + // Accessing a zero-length array must not trigger an internal compiler error. + // This code is invalid, but make sure that we can compile it. + return arr[i].n +}