Skip to content

Commit

Permalink
Merge pull request #151 from andrewei1316/dev
Browse files Browse the repository at this point in the history
split 后判断返回的数组长度避免数组访问越界
  • Loading branch information
wonderflow authored Oct 17, 2017
2 parents 2df5be0 + df2f2a9 commit 00dbadc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
16 changes: 13 additions & 3 deletions parser/grok_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ func (p *GrokParser) compile() error {
p.CustomPatterns = DEFAULT_PATTERNS + p.CustomPatterns
if len(p.CustomPatterns) != 0 {
scanner := bufio.NewScanner(strings.NewReader(p.CustomPatterns))
p.addCustomPatterns(scanner)
err := p.addCustomPatterns(scanner)
if err != nil {
return err
}
}

// Parse any custom pattern files supplied.
Expand All @@ -182,7 +185,10 @@ func (p *GrokParser) compile() error {
}

scanner := bufio.NewScanner(bufio.NewReader(file))
p.addCustomPatterns(scanner)
err = p.addCustomPatterns(scanner)
if err != nil {
return err
}
}

return p.compileCustomPatterns()
Expand Down Expand Up @@ -294,15 +300,19 @@ func (p *GrokParser) parseLine(line string) (sender.Data, error) {
return data, nil
}

func (p *GrokParser) addCustomPatterns(scanner *bufio.Scanner) {
func (p *GrokParser) addCustomPatterns(scanner *bufio.Scanner) error {
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
line = trimInvalidSpace(line)
if len(line) > 0 && line[0] != '#' {
names := strings.SplitN(line, " ", 2)
if len(names) < 2 {
return fmt.Errorf("the pattern %v is invalid, and has been ignored", line)
}
p.patterns[names[0]] = names[1]
}
}
return nil
}

func (p *GrokParser) compileCustomPatterns() error {
Expand Down
13 changes: 13 additions & 0 deletions parser/grok_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,3 +761,16 @@ func TestTrimInvalidSpace(t *testing.T) {
assert.Equal(t, ti.exp, got)
}
}

func TestAddCustomPatterns(t *testing.T) {
p := &GrokParser{
Patterns: []string{"%{TEST_LOG_A}"},
CustomPatterns: `
DURATION
RESPONSE_CODE %{NUMBER:response_code}
RESPONSE_TIME %{DURATION:response_time}
TEST_LOG_A %{NUMBER:myfloat:float} %{RESPONSE_CODE} %{IPORHOST:clientip} %{RESPONSE_TIME}
`,
}
assert.Error(t, p.compile())
}

0 comments on commit 00dbadc

Please sign in to comment.