From e1fe370fe6bc1f4ae67b77f13fb0f473f990277e Mon Sep 17 00:00:00 2001 From: Alaa Agbaria <11946121+agbaria@users.noreply.github.com> Date: Sun, 4 May 2025 16:41:20 +0300 Subject: [PATCH 1/2] fix nil pointer dereference panic when x is a nil pointer --- gomock/string.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gomock/string.go b/gomock/string.go index ec4ca7e4..b9a36ec2 100644 --- a/gomock/string.go +++ b/gomock/string.go @@ -12,6 +12,10 @@ func getString(x any) string { if isGeneratedMock(x) { return fmt.Sprintf("%T", x) } + typ := reflect.TypeOf(x) + if typ.Kind() == reflect.Ptr && typ.IsNil() { + return "nil" + } if s, ok := x.(fmt.Stringer); ok { return s.String() } From ffcef96d568c00d88f6737be760827e23d31d8c3 Mon Sep 17 00:00:00 2001 From: Alaa Agbaria <11946121+agbaria@users.noreply.github.com> Date: Sun, 4 May 2025 16:58:49 +0300 Subject: [PATCH 2/2] ValueOf instead of TypeOf --- gomock/string.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gomock/string.go b/gomock/string.go index b9a36ec2..1f6ddbca 100644 --- a/gomock/string.go +++ b/gomock/string.go @@ -12,7 +12,7 @@ func getString(x any) string { if isGeneratedMock(x) { return fmt.Sprintf("%T", x) } - typ := reflect.TypeOf(x) + typ := reflect.ValueOf(x) if typ.Kind() == reflect.Ptr && typ.IsNil() { return "nil" }