Skip to content

Commit 74a1a0f

Browse files
scorptec68mcuadros
authored andcommitted
Fix up parseUpToLen(). (#46)
The parseUpToLen function would not handle the max chars limit. Found when using a syslog client which used > max chars for the APP-NAME. I changed the syslog client to use max chars and it still didn't work. There was an off by 1 error. One of the tests even gave a max chars string and expected an error - changed that test to try with max+1 chars.
1 parent 11c69e4 commit 74a1a0f

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

format/rfc6587_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,5 @@ func (s *FormatSuite) TestRFC6587_GetSplitBadSplit(c *C) {
8585
c.Assert(r, NotNil)
8686

8787
err := scanner.Err()
88-
c.Assert(err, ErrorMatches, "strconv.ParseInt: parsing \".2\": invalid syntax")
88+
c.Assert(err, ErrorMatches, "strconv.*: parsing \".2\": invalid syntax")
8989
}

internal/syslogparser/rfc5424/rfc5424.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ func parseUpToLen(buff []byte, cursor *int, l int, maxLen int, e error) (string,
579579

580580
max := *cursor + maxLen
581581

582-
for to = *cursor; (to < max) && (to < l); to++ {
582+
for to = *cursor; (to <= max) && (to < l); to++ {
583583
if buff[to] == ' ' {
584584
found = true
585585
break
@@ -588,6 +588,8 @@ func parseUpToLen(buff []byte, cursor *int, l int, maxLen int, e error) (string,
588588

589589
if found {
590590
result = string(buff[*cursor:to])
591+
} else if (to > max) {
592+
to = max; // don't go past max
591593
}
592594

593595
*cursor = to

internal/syslogparser/rfc5424/rfc5424_test.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func (s *Rfc5424TestSuite) TestParser_Valid(c *C) {
2222
// no STRUCTURED-DATA
2323
"<34>1 2003-10-11T22:14:15.003Z mymachine.example.com su - ID47 - 'su root' failed for lonvick on /dev/pts/8",
2424
"<165>1 2003-08-24T05:14:15.000003-07:00 192.0.2.1 myproc 8710 - - %% It's time to make the do-nuts.",
25+
"<165>1 2003-08-24T05:14:15.000003-07:00 192.0.2.1 012345678901234567890123456789012345678901234567 8710 - - %% It's time to make the do-nuts.",
2526
// with STRUCTURED-DATA
2627
`<165>1 2003-10-11T22:14:15.003Z mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011"] An application event log entry...`,
2728
// STRUCTURED-DATA Only
@@ -60,6 +61,19 @@ func (s *Rfc5424TestSuite) TestParser_Valid(c *C) {
6061
"structured_data": "-",
6162
"message": "%% It's time to make the do-nuts.",
6263
},
64+
syslogparser.LogParts{
65+
"priority": 165,
66+
"facility": 20,
67+
"severity": 5,
68+
"version": 1,
69+
"timestamp": time.Date(2003, time.August, 24, 5, 14, 15, 3*10e2, tmpTs.Location()),
70+
"hostname": "192.0.2.1",
71+
"app_name": "012345678901234567890123456789012345678901234567",
72+
"proc_id": "8710",
73+
"msg_id": "-",
74+
"structured_data": "-",
75+
"message": "%% It's time to make the do-nuts.",
76+
},
6377
syslogparser.LogParts{
6478
"priority": 165,
6579
"facility": 20,
@@ -684,7 +698,7 @@ func (s *Rfc5424TestSuite) TestParseMsgId_Valid(c *C) {
684698

685699
func (s *Rfc5424TestSuite) TestParseMsgId_TooLong(c *C) {
686700
// > 32chars
687-
buff := []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ")
701+
buff := []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ")
688702
procId := ""
689703

690704
s.assertParseMsgId(c, procId, buff, 32, ErrInvalidMsgId)

0 commit comments

Comments
 (0)