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.
This commit is contained in:
Winni Neessen 2023-11-29 17:34:15 +01:00
parent da21550dc9
commit 5a4f814061
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D

View file

@ -758,7 +758,9 @@ func TestMsg_SetMessageIDWithValue(t *testing.T) {
t.Errorf("SetMessageID() failed. Expected value, got: empty") t.Errorf("SetMessageID() failed. Expected value, got: empty")
return return
} }
if _, ok := m.genHeader[HeaderMessageID]; ok {
m.genHeader[HeaderMessageID] = nil m.genHeader[HeaderMessageID] = nil
}
v := "This.is.a.message.id" v := "This.is.a.message.id"
vf := "<This.is.a.message.id>" vf := "<This.is.a.message.id>"
m.SetMessageIDWithValue(v) m.SetMessageIDWithValue(v)
@ -779,8 +781,10 @@ func TestMsg_SetMessageIDRandomness(t *testing.T) {
m := NewMsg() m := NewMsg()
m.SetMessageID() m.SetMessageID()
mid := m.GetGenHeader(HeaderMessageID) mid := m.GetGenHeader(HeaderMessageID)
if len(mid) > 0 {
mids = append(mids, mid[0]) mids = append(mids, mid[0])
} }
}
c := make(map[string]int) c := make(map[string]int)
for i := range mids { for i := range mids {
c[mids[i]]++ c[mids[i]]++