From 5a4f814061d6e599d3aa4c82c3c3859aa7f83e94 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Wed, 29 Nov 2023 17:34:15 +0100 Subject: [PATCH] Protect against key not found in genHeader map in msg.go Added checks to ensure the key HeaderMessageID exists in the genHeader map before attempting to operate on it in msg.go. This will prevent possible null pointer exceptions when trying to assign the key a value or append it to the mids slice. --- msg_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/msg_test.go b/msg_test.go index b86be9f..c90f8de 100644 --- a/msg_test.go +++ b/msg_test.go @@ -758,7 +758,9 @@ func TestMsg_SetMessageIDWithValue(t *testing.T) { t.Errorf("SetMessageID() failed. Expected value, got: empty") return } - m.genHeader[HeaderMessageID] = nil + if _, ok := m.genHeader[HeaderMessageID]; ok { + m.genHeader[HeaderMessageID] = nil + } v := "This.is.a.message.id" vf := "" m.SetMessageIDWithValue(v) @@ -779,7 +781,9 @@ func TestMsg_SetMessageIDRandomness(t *testing.T) { m := NewMsg() m.SetMessageID() mid := m.GetGenHeader(HeaderMessageID) - mids = append(mids, mid[0]) + if len(mid) > 0 { + mids = append(mids, mid[0]) + } } c := make(map[string]int) for i := range mids {