scanner token buffer can overflow use bufio.NewReader() (#4912)

To avoid the following error on binary objects

```
bufio.Scanner: token too long.
```
This commit is contained in:
Harshavardhana
2024-04-29 02:48:39 -07:00
committed by GitHub
parent 5dfaa31d67
commit afd408284e
+7 -6
View File
@@ -134,15 +134,19 @@ func headOut(r io.Reader, nlines int64) *probe.Error {
}
// Initialize a new scanner.
scn := bufio.NewScanner(r)
br := bufio.NewReader(r)
// Negative number of lines means default number of lines.
if nlines < 0 {
nlines = 10
}
for scn.Scan() && nlines > 0 {
if _, e := stdout.Write(scn.Bytes()); e != nil {
for nlines > 0 {
line, _, e := br.ReadLine()
if e != nil {
return probe.NewError(e)
}
if _, e := stdout.Write(line); e != nil {
switch e := e.(type) {
case *os.PathError:
if e.Err == syscall.EPIPE {
@@ -157,9 +161,6 @@ func headOut(r io.Reader, nlines int64) *probe.Error {
stdout.Write([]byte("\n"))
nlines--
}
if e := scn.Err(); e != nil {
return probe.NewError(e)
}
return nil
}