瀏覽代碼

[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>
Laurence Jones 1 年之前
父節點
當前提交
b8e6bd8c9a
共有 1 個文件被更改,包括 8 次插入3 次删除
  1. 8 3
      pkg/hubtest/parser_assert.go

+ 8 - 3
pkg/hubtest/parser_assert.go

@@ -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)