Skip to content

Commit

Permalink
mountinfo: move s.Err() out of scanner loop
Browse files Browse the repository at this point in the history
The previous code would actually ignore IO errors during scanning,
becuase s.Scan() returns false if there was an error (leaving the loop
and never being detected). Indeed the correct way[1] of using the
builtin bufio.Scanner is actually more like:

  s := bufio.NewScanner(...)
  for s.Scan() {
      // use s.Text()
  }
  if err := s.Err(); err != nil {
      return err
  }

[1]: https://blog.golang.org/errors-are-values

Signed-off-by: Aleksa Sarai <[email protected]>
  • Loading branch information
cyphar committed Oct 20, 2020
1 parent 95f2efb commit 99cfd57
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions mountinfo/mountinfo_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import (
func GetMountsFromReader(r io.Reader, filter FilterFunc) ([]*Info, error) {
s := bufio.NewScanner(r)
out := []*Info{}
var err error
for s.Scan() {
if err = s.Err(); err != nil {
return nil, err
}
var err error

/*
See http://man7.org/linux/man-pages/man5/proc.5.html
Expand Down Expand Up @@ -133,6 +131,9 @@ func GetMountsFromReader(r io.Reader, filter FilterFunc) ([]*Info, error) {
break
}
}
if err := s.Err(); err != nil {
return nil, err
}
return out, nil
}

Expand Down

0 comments on commit 99cfd57

Please sign in to comment.