From 83056c539613ab4059b74c429d1d2840ec1a28c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ken-H=C3=A5vard=20Lieng?= Date: Fri, 16 Dec 2016 23:49:37 +0100 Subject: [PATCH] Prevent panicing when parsing invalid messages --- irc/message.go | 8 +++++++- irc/message_test.go | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/irc/message.go b/irc/message.go index 6e0ad59f..dfec47de 100644 --- a/irc/message.go +++ b/irc/message.go @@ -19,7 +19,13 @@ func parseMessage(line string) *Message { if strings.HasPrefix(line, ":") { cmdStart = strings.Index(line, " ") + 1 - msg.Prefix = line[1 : cmdStart-1] + + if cmdStart > 0 { + msg.Prefix = line[1 : cmdStart-1] + } else { + // Invalid message + return &msg + } if i := strings.Index(msg.Prefix, "!"); i > 0 { msg.Nick = msg.Prefix[:i] diff --git a/irc/message_test.go b/irc/message_test.go index 2d66228a..9ea7b94f 100644 --- a/irc/message_test.go +++ b/irc/message_test.go @@ -71,3 +71,10 @@ func TestParseMessage(t *testing.T) { assert.Equal(t, tc.expected, parseMessage(tc.input)) } } + +func TestBadMessagePanic(t *testing.T) { + parseMessage(":user\r\n") + parseMessage(":\r\n") + parseMessage(":") + parseMessage("") +}