Skip to content

Commit 3391517

Browse files
griesemergopherbot
authored andcommitted
cmd/compile: don't crash in size computation for invalid type
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 <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent 1b86ef4 commit 3391517

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/cmd/compile/internal/noder/sizes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func (s *gcSizes) Sizeof(T types2.Type) int64 {
134134
}
135135

136136
var basicSizes = [...]byte{
137+
types2.Invalid: 1,
137138
types2.Bool: 1,
138139
types2.Int8: 1,
139140
types2.Int16: 2,

test/fixedbugs/issue52748.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// errorcheck
2+
3+
// Copyright 2022 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package p
8+
9+
import "unsafe"
10+
11+
type S[T any] struct{}
12+
13+
const c = unsafe.Sizeof(S[[c]byte]{}) // ERROR "initialization loop"

0 commit comments

Comments
 (0)