]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: don't crash in size computation for invalid type
authorRobert Griesemer <gri@golang.org>
Sat, 7 May 2022 23:18:57 +0000 (16:18 -0700)
committerGopher Robot <gobot@golang.org>
Sun, 8 May 2022 17:29:15 +0000 (17:29 +0000)
An invalid program may produce invalid types. If the program
calls unsafe.Sizeof on such a type, which is a compile-time
computation, the size-computation must be able to handle it.
Add the invalid type to the list of permissible basic types
and give it a size of 1 (word).

Fixes #52748.

Change-Id: I6c409628f9b77044758caf71cdcb199f9e77adea
Reviewed-on: https://go-review.googlesource.com/c/go/+/404894
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
src/cmd/compile/internal/noder/sizes.go
test/fixedbugs/issue52748.go [new file with mode: 0644]

index 23f206267547f4fd467fc91a93f0ab3998c2db5c..9ba0e509d79cc429c6a091e8fe8af4e6e8a33799 100644 (file)
@@ -134,6 +134,7 @@ func (s *gcSizes) Sizeof(T types2.Type) int64 {
 }
 
 var basicSizes = [...]byte{
+       types2.Invalid:    1,
        types2.Bool:       1,
        types2.Int8:       1,
        types2.Int16:      2,
diff --git a/test/fixedbugs/issue52748.go b/test/fixedbugs/issue52748.go
new file mode 100644 (file)
index 0000000..42973c0
--- /dev/null
@@ -0,0 +1,13 @@
+// errorcheck
+
+// Copyright 2022 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
+
+import "unsafe"
+
+type S[T any] struct{}
+
+const c = unsafe.Sizeof(S[[c]byte]{}) // ERROR "initialization loop"