[Explain] s02 can cause panic if empty (#2486)

* Add parsers length check as it can panic is enrich is empty

* Lets get smarter and loop backwards to find last successful stage

* Shorten code

---------

Co-authored-by: Thibault "bui" Koechlin <thibault@crowdsec.net>
This commit is contained in:
Laurence Jones 2023-09-29 12:03:56 +01:00 committed by GitHub
parent 95ed308207
commit b8e6bd8c9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -321,9 +321,14 @@ func LoadParserDump(filepath string) (*ParserResults, error) {
stages = append(stages, k)
}
sort.Strings(stages)
/*the very last one is set to 'success' which is just a bool indicating if the line was successfully parsed*/
lastStage := stages[len(stages)-2]
var lastStage string
//Loop over stages to find last successful one with at least one parser
for i := len(stages) - 2; i >= 0; i-- {
if len(pdump[stages[i]]) != 0 {
lastStage = stages[i]
break
}
}
parsers := make([]string, 0, len(pdump[lastStage]))
for k := range pdump[lastStage] {
parsers = append(parsers, k)