Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove wrong loop in matchData #785

Merged
merged 4 commits into from
May 9, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 13 additions & 15 deletions internal/corazarules/rule_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,19 @@

func (mr MatchedRule) matchData(matchData types.MatchData) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's a bit unrelated, but while we're here, can we have this function accept log *strings.Builder and pass it in from the caller instead of creating a new one?

log := &strings.Builder{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's restructure this function to something like

op := mr.Rule_.Operator()
if op == "" {
  log.WriteString("Matched.")
  return
}
// Rest of logic

for _, matchData := range mr.MatchedDatas_ {
v := matchData.Variable().Name()
if matchData.Key() != "" {
v += fmt.Sprintf(":%s", matchData.Key())
}
value := matchData.Value()
if len(value) > 200 {
value = value[:200]
}
if mr.Rule_.Operator() != "" {
log.WriteString(fmt.Sprintf("Matched \"Operator %s matched %s at %s.",
"", value, v))
} else {
log.WriteString("Matched.\"")
}
v := matchData.Variable().Name()
if matchData.Key() != "" {
v += fmt.Sprintf(":%s", matchData.Key())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to remove the intermediate strings, while it's more lines I guess it's still not so bad. We're basically expanding printf into code, which should be worth it for this very hot path function

log.WriteString("Matched Operator ")
log.WriteString(op)
log.WriteString(" matched ")
log.WriteString(value)
log.WriteString(" at ")
log.WriteString(matchData.Variable().Name())
if matchData.Key() != "" {
  log.WriteString(":")
  log.WriteString(matchData.Key())
}
log.WriteString(".")

}
value := matchData.Value()
Hayak3 marked this conversation as resolved.
Show resolved Hide resolved
if len(value) > 200 {
value = value[:200]
}
if mr.Rule_.Operator() != "" {
log.WriteString(fmt.Sprintf("Matched \"Operator %s matched %s at %s.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need the first interpolation here @jptosso

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a bug, let's fix it by interpolating the operator

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW I'm wondering whether \" is needed here and below, it seems to be an unterminated quote

"", value, v))
} else {
log.WriteString("Matched.\"")

Check warning on line 145 in internal/corazarules/rule_match.go

View check run for this annotation

Codecov / codecov/patch

internal/corazarules/rule_match.go#L133-L145

Added lines #L133 - L145 were not covered by tests
}
return log.String()
}
Expand Down