Skip to content

Commit

Permalink
Fix #6 - don't panic when len(Message)<2
Browse files Browse the repository at this point in the history
  • Loading branch information
SjonHortensius committed May 13, 2019
1 parent ced89e0 commit da2ff92
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion SystemdJournal2Gelf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions SystemdJournal2Gelf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) {
Expand Down

0 comments on commit da2ff92

Please sign in to comment.