Skip to content

Commit 770b141

Browse files
authored
perf(misconf): Improve cause performance (aquasecurity#6586)
Signed-off-by: Simar <[email protected]>
1 parent 3ccb1a0 commit 770b141

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

pkg/iac/scan/code.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package scan
22

33
import (
4+
"bufio"
5+
"bytes"
46
"fmt"
57
"io/fs"
68
"path/filepath"
@@ -149,7 +151,14 @@ func (r *Result) GetCode(opts ...CodeOption) (*Code, error) {
149151
Lines: nil,
150152
}
151153

152-
rawLines := strings.Split(string(content), "\n")
154+
var rawLines []string
155+
bs := bufio.NewScanner(bytes.NewReader(content))
156+
for bs.Scan() {
157+
rawLines = append(rawLines, bs.Text())
158+
}
159+
if bs.Err() != nil {
160+
return nil, fmt.Errorf("failed to scan file : %w", err)
161+
}
153162

154163
var highlightedLines []string
155164
if settings.includeHighlighted {

pkg/misconf/scanner.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -503,20 +503,26 @@ func NewCauseWithCode(underlying scan.Result) types.CauseMetadata {
503503
},
504504
})
505505
}
506-
if code, err := underlying.GetCode(); err == nil {
507-
cause.Code = types.Code{
508-
Lines: lo.Map(code.Lines, func(l scan.Line, i int) types.Line {
509-
return types.Line{
510-
Number: l.Number,
511-
Content: l.Content,
512-
IsCause: l.IsCause,
513-
Annotation: l.Annotation,
514-
Truncated: l.Truncated,
515-
Highlighted: l.Highlighted,
516-
FirstCause: l.FirstCause,
517-
LastCause: l.LastCause,
518-
}
519-
}),
506+
507+
// only failures have a code cause
508+
// failures can happen either due to lack of
509+
// OR misconfiguration of something
510+
if underlying.Status() == scan.StatusFailed {
511+
if code, err := underlying.GetCode(); err == nil {
512+
cause.Code = types.Code{
513+
Lines: lo.Map(code.Lines, func(l scan.Line, i int) types.Line {
514+
return types.Line{
515+
Number: l.Number,
516+
Content: l.Content,
517+
IsCause: l.IsCause,
518+
Annotation: l.Annotation,
519+
Truncated: l.Truncated,
520+
Highlighted: l.Highlighted,
521+
FirstCause: l.FirstCause,
522+
LastCause: l.LastCause,
523+
}
524+
}),
525+
}
520526
}
521527
}
522528
return cause

0 commit comments

Comments
 (0)