Skip to content

Commit

Permalink
Split up rule warning/error handling to avoid SEGFAULT (#270)
Browse files Browse the repository at this point in the history
* Add test file

* Revert "Add test file"

This reverts commit 01102fc.

* Split up rule warning/error handling to avoid SEGFAULT

* Avoid lint issue by collecting errors and returning all of their text
  • Loading branch information
tstromberg authored Jun 21, 2024
1 parent 69b9ddf commit f5d91c0
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions pkg/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,31 @@ func Recursive(ctx context.Context, fss []fs.FS) (*yara.Rules, error) {
}

if err != nil {
return nil, fmt.Errorf("walk: %w", err)
return nil, err
}

warnings := map[string]string{}
for _, ycw := range yc.Warnings {
clog.WarnContext(ctx, "warning", slog.String("namespace", ycw.Rule.Namespace()), slog.String("warning", ycw.Text), slog.String("id", ycw.Rule.Identifier()))
clog.WarnContext(ctx, "warning", slog.String("filename", ycw.Filename), slog.Int("line", ycw.Line), slog.String("text", ycw.Text))
if ycw.Rule == nil {
continue
}

id := fmt.Sprintf("%s:%s", ycw.Rule.Namespace(), ycw.Rule.Identifier())
clog.WarnContext(ctx, "rule has warning", "id", id)
warnings[id] = ycw.Text
}

errors := []string{}
for _, yce := range yc.Errors {
clog.ErrorContext(ctx, "errors", slog.String("namespace", yce.Rule.Namespace()), slog.String("error", yce.Text), slog.String("id", yce.Rule.Identifier()))
return nil, fmt.Errorf("rule error: %v", yce.Text)
clog.ErrorContext(ctx, "error", slog.String("filename", yce.Filename), slog.Int("line", yce.Line), slog.String("text", yce.Text))
if yce.Rule != nil {
clog.ErrorContext(ctx, "defective rule", slog.String("namespace", yce.Rule.Namespace()), slog.String("id", yce.Rule.Identifier()))
}
errors = append(errors, yce.Text)
}
if len(errors) > 0 {
return nil, fmt.Errorf("compile errors encountered: %v", errors)
}

rs, err := yc.GetRules()
Expand Down

0 comments on commit f5d91c0

Please sign in to comment.