Skip to content

Commit 70b277c

Browse files
committed
cmd/compile: only check return for valid functions
CheckReturn uses fn.Type() unconditionally, so for invalid function, fn.Type() will be nil, causes the compiler crashes. Updates #43311 Change-Id: I4420dd296c72ea83986b38fbf2c7f51fa59757c8 Reviewed-on: https://go-review.googlesource.com/c/go/+/298709 Trust: Cuong Manh Le <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent d4247f5 commit 70b277c

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/cmd/compile/internal/typecheck/typecheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2104,7 +2104,7 @@ func CheckUnused(fn *ir.Func) {
21042104

21052105
// CheckReturn makes sure that fn terminates appropriately.
21062106
func CheckReturn(fn *ir.Func) {
2107-
if fn.Type().NumResults() != 0 && len(fn.Body) != 0 {
2107+
if fn.Type() != nil && fn.Type().NumResults() != 0 && len(fn.Body) != 0 {
21082108
markBreak(fn)
21092109
if !isTermNodes(fn.Body) {
21102110
base.ErrorfAt(fn.Endlineno, "missing return at end of function")

test/fixedbugs/issue17588.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// errorcheck
1+
// errorcheck -d=panic
22

33
// Copyright 2016 The Go Authors. All rights reserved.
44
// Use of this source code is governed by a BSD-style
@@ -11,10 +11,10 @@
1111

1212
package p
1313

14-
type F func(b T) // ERROR "T .*is not a type|expected type"
14+
type F func(b T) // ERROR "T .*is not a type|expected type"
1515

1616
func T(fn F) {
17-
func() {
18-
fn(nil) // If Decldepth is not initialized properly, typecheckclosure() Fatals here.
19-
}()
17+
func() {
18+
fn(nil) // If Decldepth is not initialized properly, typecheckclosure() Fatals here.
19+
}()
2020
}

0 commit comments

Comments
 (0)