From c3052b491f81a4ba6cf21785c55d4809254daa32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Sun, 21 Apr 2024 20:13:28 +0200 Subject: [PATCH] parser: Check and prompt for empty patterns and region properties (fix crash) (#3256) * parser: Precise error message for missing `start` & `end` in region * parser: Check and prompt for empty patterns and region properties * syntax: Remove empty identifier pattern from log definition --- pkg/highlight/parser.go | 50 ++++++++++++++++++++++++++++++++--------- runtime/syntax/log.yaml | 2 -- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/pkg/highlight/parser.go b/pkg/highlight/parser.go index ce71c078..aab79e7c 100644 --- a/pkg/highlight/parser.go +++ b/pkg/highlight/parser.go @@ -355,6 +355,10 @@ func parseRules(input []interface{}, curRegion *region) (ru *rules, err error) { switch object := val.(type) { case string: + if object == "" { + return nil, fmt.Errorf("Empty rule %s", k) + } + if k == "include" { ru.includes = append(ru.includes, object) } else { @@ -408,30 +412,56 @@ func parseRegion(group string, regionInfo map[interface{}]interface{}, prevRegio r.group = groupNum r.parent = prevRegion - r.start, err = regexp.Compile(regionInfo["start"].(string)) + // start is mandatory + if start, ok := regionInfo["start"]; ok { + start := start.(string) + if start == "" { + return nil, fmt.Errorf("Empty start in %s", group) + } - if err != nil { - return nil, err + r.start, err = regexp.Compile(start) + if err != nil { + return nil, err + } + } else { + return nil, fmt.Errorf("Missing start in %s", group) } - r.end, err = regexp.Compile(regionInfo["end"].(string)) + // end is mandatory + if end, ok := regionInfo["end"]; ok { + end := end.(string) + if end == "" { + return nil, fmt.Errorf("Empty end in %s", group) + } - if err != nil { - return nil, err + r.end, err = regexp.Compile(end) + if err != nil { + return nil, err + } + } else { + return nil, fmt.Errorf("Missing end in %s", group) } // skip is optional - if _, ok := regionInfo["skip"]; ok { - r.skip, err = regexp.Compile(regionInfo["skip"].(string)) + if skip, ok := regionInfo["skip"]; ok { + skip := skip.(string) + if skip == "" { + return nil, fmt.Errorf("Empty skip in %s", group) + } + r.skip, err = regexp.Compile(skip) if err != nil { return nil, err } } // limit-color is optional - if _, ok := regionInfo["limit-group"]; ok { - groupStr := regionInfo["limit-group"].(string) + if groupStr, ok := regionInfo["limit-group"]; ok { + groupStr := groupStr.(string) + if groupStr == "" { + return nil, fmt.Errorf("Empty limit-group in %s", group) + } + if _, ok := Groups[groupStr]; !ok { numGroups++ Groups[groupStr] = numGroups diff --git a/runtime/syntax/log.yaml b/runtime/syntax/log.yaml index fa01238f..34ef765d 100644 --- a/runtime/syntax/log.yaml +++ b/runtime/syntax/log.yaml @@ -62,8 +62,6 @@ rules: # - identifier: "^([0-2][0-9][0-2][0-9][-/]?[0-9][0-9][-/]?[0-9][0-9]\\s[0-9][0-9]:[0-9][0-9](:[0-9][0-9])?(\\.?[0-9][0-9][0-9])?)" - identifier: "^(\\d{4}[-/]?\\d{2}[-/]?\\d{2}\\s\\d{2}:\\d{2}(:\\d{2})?(\\.?\\d{2,8})?)" - identifier: "^([0-2][0-9]|[0-2]-?[0-9][0-9]-?[0-9][0-9])\\-([0-1][0-9])\\-([0-3][0-9]) ([0-2][0-9])\\:([0-5][0-9])\\:([0-5][0-9]),([0-9][0-9][0-9])" -# ISO 8601:2004(E) -- identifier: "" # Complete precision: - identifier: "^(\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\d\\.\\d+([+-][0-2]\\d:[0-5]\\d|Z))" # No milliseconds: