From da2ff92ca6ab63cb48655b84edc555d3f74c83ed Mon Sep 17 00:00:00 2001 From: Sjon Hortensius Date: Mon, 13 May 2019 10:41:16 +0200 Subject: [PATCH] Fix #6 - don't panic when len(Message)<2 --- SystemdJournal2Gelf.go | 2 +- SystemdJournal2Gelf_test.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/SystemdJournal2Gelf.go b/SystemdJournal2Gelf.go index 5b0cb9b..edd6e36 100644 --- a/SystemdJournal2Gelf.go +++ b/SystemdJournal2Gelf.go @@ -125,7 +125,7 @@ func (this *SystemdJournalEntry) send() { } func (this *SystemdJournalEntry) isJsonMessage() bool { - return this.Message[0:2] == `{"` + return len(this.Message) > 4 && this.Message[0:2] == `{"` } type pendingEntry struct { diff --git a/SystemdJournal2Gelf_test.go b/SystemdJournal2Gelf_test.go index c302414..8819df2 100644 --- a/SystemdJournal2Gelf_test.go +++ b/SystemdJournal2Gelf_test.go @@ -117,6 +117,20 @@ func TestDateStrippedFromMessage(t *testing.T) { AssertEquals(t, 4, len(gelf.Extra)) } +func TestShortLinesDontTriggerPanic(t *testing.T) { + entry := SystemdJournalEntry{} + + err := json.Unmarshal([]byte(`{ + "MESSAGE" : "" + }`), &entry) + + AssertNotError(t, err) + + gelf := entry.toGelf() + + AssertEquals(t, "", gelf.Short) +} + // asserts func AssertEquals(t *testing.T, expected, actual interface{}) {