Skip to content

Commit affb276

Browse files
Ben Fullerleodido
Ben Fuller
authored andcommitted
return better error when message is too long
1 parent efe45e3 commit affb276

File tree

3 files changed

+12
-62
lines changed

3 files changed

+12
-62
lines changed

octetcounting/parser.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ func (p *parser) run() {
8686
break
8787
}
8888

89+
if int(p.s.msglen) > p.maxMessageLength {
90+
p.emit(&syslog.Result{
91+
Error: fmt.Errorf("message too long to parse. was size %d, max length %d", p.s.msglen, p.maxMessageLength),
92+
})
93+
break
94+
}
95+
8996
// Next we MUST see a WS
9097
if tok = p.scan(); tok.typ != WS {
9198
p.emit(&syslog.Result{

octetcounting/parser_test.go

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -565,69 +565,15 @@ func getTestCases() []testCase {
565565
},
566566
},
567567
{
568-
descr: "1st ml//maxlen gt 8192", // maxlength greather than the buffer size
569-
input: fmt.Sprintf(
570-
"8193 <%d>%d %s %s %s %s %s - %s",
571-
syslogtesting.MaxPriority,
572-
syslogtesting.MaxVersion,
573-
syslogtesting.MaxRFC3339MicroTimestamp,
574-
string(syslogtesting.MaxHostname),
575-
string(syslogtesting.MaxAppname),
576-
string(syslogtesting.MaxProcID),
577-
string(syslogtesting.MaxMsgID),
578-
string(syslogtesting.MaxMessage),
579-
),
580-
// results w/o best effort
568+
descr: "MSGLEN gt max message length",
569+
input: "16 <1>1 - - - - - -",
581570
results: []syslog.Result{
582-
{
583-
Error: fmt.Errorf(
584-
"found %s after \"%s\", expecting a %s containing %d octets",
585-
EOF,
586-
fmt.Sprintf(
587-
"<%d>%d %s %s %s %s %s - %s", syslogtesting.MaxPriority,
588-
syslogtesting.MaxVersion,
589-
syslogtesting.MaxRFC3339MicroTimestamp,
590-
string(syslogtesting.MaxHostname),
591-
string(syslogtesting.MaxAppname),
592-
string(syslogtesting.MaxProcID),
593-
string(syslogtesting.MaxMsgID),
594-
string(syslogtesting.MaxMessage),
595-
),
596-
SYSLOGMSG,
597-
8193,
598-
),
599-
},
571+
{Error: fmt.Errorf("message too long to parse. was size %d, max length %d", 16, 10)},
600572
},
601-
// results with best effort
602573
bestEffortResults: []syslog.Result{
603-
{
604-
Message: (&rfc5424.SyslogMessage{}).
605-
SetPriority(syslogtesting.MaxPriority).
606-
SetVersion(syslogtesting.MaxVersion).
607-
SetTimestamp(syslogtesting.MaxRFC3339MicroTimestamp).
608-
SetHostname(string(syslogtesting.MaxHostname)).
609-
SetAppname(string(syslogtesting.MaxAppname)).
610-
SetProcID(string(syslogtesting.MaxProcID)).
611-
SetMsgID(string(syslogtesting.MaxMsgID)).
612-
SetMessage(string(syslogtesting.MaxMessage)),
613-
Error: fmt.Errorf(
614-
"found %s after \"%s\", expecting a %s containing %d octets",
615-
EOF,
616-
fmt.Sprintf(
617-
"<%d>%d %s %s %s %s %s - %s", syslogtesting.MaxPriority,
618-
syslogtesting.MaxVersion,
619-
syslogtesting.MaxRFC3339MicroTimestamp,
620-
string(syslogtesting.MaxHostname),
621-
string(syslogtesting.MaxAppname),
622-
string(syslogtesting.MaxProcID),
623-
string(syslogtesting.MaxMsgID),
624-
string(syslogtesting.MaxMessage),
625-
),
626-
SYSLOGMSG,
627-
8193,
628-
),
629-
},
574+
{Error: fmt.Errorf("message too long to parse. was size %d, max length %d", 16, 10)},
630575
},
576+
maxMessageLength: 10,
631577
},
632578
{
633579
descr: "1st uf/2nd ok//incomplete SYSLOGMSG/notdetectable",

octetcounting/scanner.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ func (s *Scanner) scanMsgLen() Token {
113113
msglen := buf.String()
114114
s.msglen, _ = strconv.ParseUint(msglen, 10, 64)
115115

116-
// (todo) > return ILLEGAL if s.msglen > size (8192)
117-
// (todo) > only when NOT in besteffort mode or always?
118-
119116
return Token{
120117
typ: MSGLEN,
121118
lit: buf.Bytes(),

0 commit comments

Comments
 (0)