fix(detect): handle EOF with bytes (#1472)

This commit is contained in:
Richard Gomez authored and GitHub committed 2024-08-19 14:00:47 -05:00
1 parent 8a1ca9ecc9
commit f361c5ef71
2 files changed
+77 -11

No files matched your search

+16 -11
View File
@@ -15,23 +15,28 @@ func (d *Detector) DetectReader(r io.Reader, bufSize int) ([]report.Finding, err
for {
n, err := reader.Read(buf[:cap(buf)])
buf = buf[:n]
// "Callers should always process the n > 0 bytes returned before considering the error err."
// https://pkg.go.dev/io#Reader
if n > 0 {
buf = buf[:n]
fragment := Fragment{
Raw: string(buf),
}
for _, finding := range d.Detect(fragment) {
findings = append(findings, finding)
if d.Verbose {
printFinding(finding, d.NoColor)
}
}
}
if err != nil {
if err != io.EOF {
return findings, err
}
break
}
fragment := Fragment{
Raw: string(buf),
}
for _, finding := range d.Detect(fragment) {
findings = append(findings, finding)
if d.Verbose {
printFinding(finding, d.NoColor)
}
}
}
return findings, nil