From 4a1931756f9aa96c31ab9c3616ae757d03c611e7 Mon Sep 17 00:00:00 2001 From: rootkiller6788 Date: Thu, 3 Sep 2026 23:02:36 +0800 Subject: [PATCH 1/2] cmp: guard AllowUnexported against nil arguments Passing a bare nil to AllowUnexported used to panic with a runtime nil pointer dereference from reflect.Type.Kind, instead of the descriptive "invalid struct type" panic every other invalid input gets. Check for a nil type before reading its kind, like cmpopts does in the same situation. --- cmp/options.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmp/options.go b/cmp/options.go index 3e66674..d029da3 100644 --- a/cmp/options.go +++ b/cmp/options.go @@ -431,7 +431,7 @@ func AllowUnexported(types ...any) Option { m := make(map[reflect.Type]bool) for _, typ := range types { t := reflect.TypeOf(typ) - if t.Kind() != reflect.Struct { + if t == nil || t.Kind() != reflect.Struct { panic(fmt.Sprintf("invalid struct type: %T", typ)) } m[t] = true From 683cde72fa149bc513eff9e24fcee7d2e3919c01 Mon Sep 17 00:00:00 2001 From: rootkiller6788 Date: Thu, 3 Sep 2026 23:02:36 +0800 Subject: [PATCH 2/2] cmp: add regression test for AllowUnexported(nil) --- cmp/options_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/cmp/options_test.go b/cmp/options_test.go index b58f56e..e5e3576 100644 --- a/cmp/options_test.go +++ b/cmp/options_test.go @@ -214,3 +214,19 @@ func TestOptionPanic(t *testing.T) { }) } } + +// TestAllowUnexportedNilPanic asserts that AllowUnexported(nil) panics with +// the same descriptive message as other invalid inputs rather than crashing +// with a nil pointer dereference. TestOptionPanic cannot cover this case +// because it invokes the option functions through reflection, which cannot +// represent a bare nil argument. +func TestAllowUnexportedNilPanic(t *testing.T) { + var gotPanic any + func() { + defer func() { gotPanic = recover() }() + AllowUnexported(nil) + }() + if s, ok := gotPanic.(string); !ok || !strings.Contains(s, "invalid struct type") { + t.Fatalf("panic = %v (type %T), want string containing %q", gotPanic, gotPanic, "invalid struct type") + } +}