Prevent panicing when parsing invalid messages

This commit is contained in:
Ken-Håvard Lieng 2016-12-16 23:49:37 +01:00
parent cf759883aa
commit 83056c5396
2 changed files with 14 additions and 1 deletions

View File

@ -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]

View File

@ -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("")
}