From 0b65b02ba5e0eaa517509a940bb10e10abc0287a Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Sat, 1 Jul 2023 07:01:11 +0700 Subject: [PATCH] cmd/compile: fix clear on slice with zero size elem Fixed #61127 Change-Id: If07b04ebcc98438c66f273c0c94bea1f230dc2e8 Reviewed-on: https://go-review.googlesource.com/c/go/+/507535 Run-TryBot: Cuong Manh Le Reviewed-by: Keith Randall Reviewed-by: Cherry Mui TryBot-Result: Gopher Robot Reviewed-by: Matthew Dempsky Auto-Submit: Cuong Manh Le --- src/cmd/compile/internal/walk/builtin.go | 6 +++++- test/fixedbugs/issue61127.go | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/fixedbugs/issue61127.go diff --git a/src/cmd/compile/internal/walk/builtin.go b/src/cmd/compile/internal/walk/builtin.go index 5c924a90c5..786c31313c 100644 --- a/src/cmd/compile/internal/walk/builtin.go +++ b/src/cmd/compile/internal/walk/builtin.go @@ -135,7 +135,11 @@ func walkClear(n *ir.UnaryExpr) ir.Node { typ := n.X.Type() switch { case typ.IsSlice(): - return arrayClear(n.X.Pos(), n.X, nil) + if n := arrayClear(n.X.Pos(), n.X, nil); n != nil { + return n + } + // If n == nil, we are clearing an array which takes zero memory, do nothing. + return ir.NewBlockStmt(n.Pos(), nil) case typ.IsMap(): return mapClear(n.X, reflectdata.TypePtrAt(n.X.Pos(), n.X.Type())) } diff --git a/test/fixedbugs/issue61127.go b/test/fixedbugs/issue61127.go new file mode 100644 index 0000000000..c8ee5c5ee4 --- /dev/null +++ b/test/fixedbugs/issue61127.go @@ -0,0 +1,13 @@ +// compile + +// Copyright 2023 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 + +var V = []struct{}{} + +func main() { + clear(V) +} -- 2.48.1