From 2ef7eef1726a6caac2b67ffce538d374a8765373 Mon Sep 17 00:00:00 2001 From: Mauri de Souza Meneguzzo Date: Sun, 29 Oct 2023 20:51:34 -0300 Subject: [PATCH 1/3] runtime: add missing runtime error prefix to PanicNilError Errors that implement runtime.Error should have a "runtime error: " prefix, with the solo exception of runtime.plainError (on purpose). Calling panic(nil) results in a PanicNilError that violates this constraint. This CL changes the error from panic(nil) from: panic called with nil argument to runtime error: panic called with nil argument Fixes #63813 --- src/runtime/panic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/panic.go b/src/runtime/panic.go index 5f54ee4b012d7b..928b75cb7c703f 100644 --- a/src/runtime/panic.go +++ b/src/runtime/panic.go @@ -704,7 +704,7 @@ type PanicNilError struct { _ [0]*PanicNilError } -func (*PanicNilError) Error() string { return "panic called with nil argument" } +func (*PanicNilError) Error() string { return "runtime error: panic called with nil argument" } func (*PanicNilError) RuntimeError() {} var panicnil = &godebugInc{name: "panicnil"} From d04cb4bf67e524bc55a85fbd9ba0a371e45cbb8c Mon Sep 17 00:00:00 2001 From: Mauri de Souza Meneguzzo Date: Wed, 13 Dec 2023 18:16:26 -0300 Subject: [PATCH 2/3] add test --- src/runtime/panic_test.go | 54 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/runtime/panic_test.go b/src/runtime/panic_test.go index b8a300f6b10c20..13b6d5326aacb6 100644 --- a/src/runtime/panic_test.go +++ b/src/runtime/panic_test.go @@ -5,6 +5,7 @@ package runtime_test import ( + "runtime" "strings" "testing" ) @@ -46,3 +47,56 @@ func TestPanicWithDirectlyPrintableCustomTypes(t *testing.T) { }) } } + +func TestPanicNilErrorPrefix(t *testing.T) { + tests := []struct { + name string + wantPanic string + fn func() + }{ + { + name: "panic(nil)", + fn: func() { + panic(nil) + }, + }, + { + name: "panic((any)(nil))", + fn: func() { + var foo any = nil + panic(foo) + }, + }, + { + name: "panic((error)(nil))", + fn: func() { + var err error + panic(err) + }, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + defer func() { + r := recover() + if r == nil { + t.Fatal("expected a panic") + } + + re, ok := r.(runtime.Error) + if !ok { + t.Fatalf("wrong panic type: got %T, want runtime.Error", r) + } + got := re.Error() + want := "runtime error: panic called with nil argument" + if !strings.Contains(got, want) { + t.Fatalf("got: %s, want: %s", got, want) + } + }() + + tt.fn() + }) + } +} From 69738780be374af8f410afef9406b57339b959fa Mon Sep 17 00:00:00 2001 From: Mauri de Souza Meneguzzo Date: Wed, 13 Dec 2023 19:39:45 -0300 Subject: [PATCH 3/3] fix panic test for old issue --- test/fixedbugs/issue19658.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fixedbugs/issue19658.go b/test/fixedbugs/issue19658.go index 1e13573b72bc97..7d2dc5ae5cd93b 100644 --- a/test/fixedbugs/issue19658.go +++ b/test/fixedbugs/issue19658.go @@ -47,7 +47,7 @@ func main() { Input string Expect string }{ - {"", "nil", "panic: panic called with nil argument"}, + {"", "nil", "panic: runtime error: panic called with nil argument"}, {"errors.New", `"test"`, "panic: test"}, {"S", "S{}", "panic: s-stringer"}, {"byte", "8", "panic: 8"},