From 3391517c0e4695a87cdb806cbf7b9760e7c9fa73 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Sat, 7 May 2022 16:18:57 -0700 Subject: [PATCH] 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 Reviewed-by: Cuong Manh Le Reviewed-by: Robert Griesemer Auto-Submit: Robert Griesemer TryBot-Result: Gopher Robot Reviewed-by: David Chase --- src/cmd/compile/internal/noder/sizes.go | 1 + test/fixedbugs/issue52748.go | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 test/fixedbugs/issue52748.go diff --git a/src/cmd/compile/internal/noder/sizes.go b/src/cmd/compile/internal/noder/sizes.go index 23f2062675..9ba0e509d7 100644 --- a/src/cmd/compile/internal/noder/sizes.go +++ b/src/cmd/compile/internal/noder/sizes.go @@ -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 index 0000000000..42973c01e5 --- /dev/null +++ b/test/fixedbugs/issue52748.go @@ -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" -- 2.50.0