Browse Source

fixed parser bug when handling empty strings, added empty string check to email alarm

master
Christopher Ramey 3 years ago
parent
commit
3a4317f3a9
  1. 6
      alarm/alarm_email.go
  2. 17
      config/parser.go

6
alarm/alarm_email.go

@ -44,6 +44,9 @@ func (a *AlarmEmail) Parse(tk string) (bool, error) {
case "smtp": case "smtp":
a.state = TK_SMTP a.state = TK_SMTP
default: default:
if len(a.To) < 1 {
return false, fmt.Errorf("email alarm requires to address")
}
return false, nil return false, nil
} }
@ -56,6 +59,9 @@ func (a *AlarmEmail) Parse(tk string) (bool, error) {
a.state = TK_NONE a.state = TK_NONE
case TK_TO: case TK_TO:
if strings.TrimSpace(tk) == "" {
return false, fmt.Errorf("to address cannot be empty")
}
a.To = append(a.To, tk) a.To = append(a.To, tk)
a.state = TK_NONE a.state = TK_NONE

17
config/parser.go

@ -276,7 +276,7 @@ func (p *Parser) Split(data []byte, atEOF bool) (int, []byte, error) {
for i := 0; i < len(data); i++ { for i := 0; i < len(data); i++ {
c := data[i] c := data[i]
// fmt.Printf("%c (%t) (%t)\n", c, started, ignoreline)
//fmt.Printf("%c (%t) (%t)\n", c, started, ignoreline)
switch c { switch c {
case '\f', '\n', '\r': case '\f', '\n', '\r':
p.Line++ p.Line++
@ -292,10 +292,17 @@ func (p *Parser) Split(data []byte, atEOF bool) (int, []byte, error) {
} }
case '\'', '"', '`': case '\'', '"', '`':
if started && quote == c {
return i + 1, data[startidx:i], nil
// When the quote ends
if quote == c {
// if we've gotten data, return it
if started {
return i + 1, data[startidx:i], nil
}
// if we haven't return nothing
return i + 1, []byte{}, nil
} }
// start a quoted string
if !ignoreline && quote == 0 { if !ignoreline && quote == 0 {
quote = c quote = c
} }
@ -314,6 +321,10 @@ func (p *Parser) Split(data []byte, atEOF bool) (int, []byte, error) {
} }
if atEOF { if atEOF {
if quote != 0 {
return 0, nil, fmt.Errorf("unterminated quote")
}
if ignoreline { if ignoreline {
return len(data), nil, nil return len(data), nil, nil
} }

Loading…
Cancel
Save