From 3cde835bb2edf82ce27c91ba2edd8bd8de4a0f54 Mon Sep 17 00:00:00 2001 From: yacovm Date: Mon, 17 Jul 2017 17:01:19 +0300 Subject: [PATCH] [FAB-5339] Add missing nil check to extensions.go There is a missing nil check, that might make the code result in a null pointer panic. For reference: IsDataMsg is: func (m *GossipMessage) IsDataMsg() bool { return m.GetDataMsg() != nil } Change-Id: I5e20dee188e863b932312a7963f0ca73965c1457 Signed-off-by: yacovm --- protos/gossip/extensions.go | 2 +- protos/gossip/extensions_test.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/protos/gossip/extensions.go b/protos/gossip/extensions.go index 0adb1d08b98..012a0e600d1 100644 --- a/protos/gossip/extensions.go +++ b/protos/gossip/extensions.go @@ -521,7 +521,7 @@ func (m *SignedGossipMessage) String() string { var isSimpleMsg bool if m.GetStateResponse() != nil { gMsg = fmt.Sprintf("StateResponse with %d items", len(m.GetStateResponse().Payloads)) - } else if m.IsDataMsg() { + } else if m.IsDataMsg() && m.GetDataMsg().Payload != nil { gMsg = m.GetDataMsg().Payload.toString() } else if m.IsDataUpdate() { update := m.GetDataUpdate() diff --git a/protos/gossip/extensions_test.go b/protos/gossip/extensions_test.go index 9a8bccf84b4..362e80334f0 100644 --- a/protos/gossip/extensions_test.go +++ b/protos/gossip/extensions_test.go @@ -72,6 +72,10 @@ func TestToString(t *testing.T) { }, } assert.NotContains(t, fmt.Sprintf("%v", sMsg), "2") + sMsg.GetDataMsg().Payload = nil + assert.NotPanics(t, func() { + _ = sMsg.String() + }) sMsg = &SignedGossipMessage{ GossipMessage: &GossipMessage{