From 416dce92f3e6fa1d4892bf8c3f0a1973d4b9f7c8 Mon Sep 17 00:00:00 2001 From: Samuel Stauffer Date: Thu, 29 Jun 2023 19:45:06 -0700 Subject: [PATCH] Fix panic when parsing line with only priority. parseHeader was not checking that the end of the input hasn't been reached, and parseTimestamp and parseHostname assume that there's at least one character available. Avoid a panic by checking in parseHeader, and add a test to verify. --- internal/syslogparser/rfc3164/rfc3164.go | 4 +++- internal/syslogparser/rfc3164/rfc3164_test.go | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/internal/syslogparser/rfc3164/rfc3164.go b/internal/syslogparser/rfc3164/rfc3164.go index 486a0cb..00ef02f 100644 --- a/internal/syslogparser/rfc3164/rfc3164.go +++ b/internal/syslogparser/rfc3164/rfc3164.go @@ -105,7 +105,9 @@ func (p *Parser) parsePriority() (syslogparser.Priority, error) { func (p *Parser) parseHeader() (header, error) { hdr := header{} - var err error + if p.cursor >= p.l { + return hdr, syslogparser.ErrEOL + } ts, err := p.parseTimestamp() if err != nil { diff --git a/internal/syslogparser/rfc3164/rfc3164_test.go b/internal/syslogparser/rfc3164/rfc3164_test.go index 91a245e..128f5a2 100644 --- a/internal/syslogparser/rfc3164/rfc3164_test.go +++ b/internal/syslogparser/rfc3164/rfc3164_test.go @@ -310,6 +310,23 @@ func (s *Rfc3164TestSuite) TestParseContent_Valid(c *C) { c.Assert(p.cursor, Equals, len(content)) } +func (s *Rfc3164TestSuite) TestParser_PriorityOnly(c *C) { + buff := []byte("<34>") + + p := NewParser(buff) + expectedP := &Parser{ + buff: buff, + cursor: 0, + l: len(buff), + location: time.UTC, + } + + c.Assert(p, DeepEquals, expectedP) + + err := p.Parse() + c.Assert(err, Equals, syslogparser.ErrEOL) +} + func (s *Rfc3164TestSuite) BenchmarkParseTimestamp(c *C) { buff := []byte("Oct 11 22:14:15")