]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile: fix clear on slice with zero size elem
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Sat, 1 Jul 2023 00:01:11 +0000 (07:01 +0700)
committerKeith Randall <khr@golang.org>
Mon, 10 Jul 2023 16:31:54 +0000 (16:31 +0000)
Fixed #61127

Change-Id: If07b04ebcc98438c66f273c0c94bea1f230dc2e8
Reviewed-on: https://go-review.googlesource.com/c/go/+/507535
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>

src/cmd/compile/internal/walk/builtin.go
test/fixedbugs/issue61127.go [new file with mode: 0644]

index 5c924a90c5cbe848d8363dcc66adbd23c876afa3..786c31313c4172f6bd4424c120deb6aa93f36bed 100644 (file)
@@ -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 (file)
index 0000000..c8ee5c5
--- /dev/null
@@ -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)
+}