From 3a71eb02567c4f248e85c208f176bd75255f0cc8 Mon Sep 17 00:00:00 2001 From: Wang Long Date: Tue, 17 Jan 2017 21:25:00 +0800 Subject: [PATCH] move error check out of the for loop The `bufio.Scanner.Scan` method returns false either by reaching the end of the input or an error. After Scan returns false, the Err method will return any error that occurred during scanning, except that if it was io.EOF, Err will return nil. We should check the error when Scan return false(out of the for loop). Signed-off-by: Wang Long --- libcontainer/cgroups/utils.go | 14 +++++++------- libcontainer/seccomp/seccomp_linux.go | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/libcontainer/cgroups/utils.go b/libcontainer/cgroups/utils.go index 9f9105a5..12242bd1 100644 --- a/libcontainer/cgroups/utils.go +++ b/libcontainer/cgroups/utils.go @@ -189,9 +189,6 @@ func GetAllSubsystems() ([]string, error) { s := bufio.NewScanner(f) for s.Scan() { - if err := s.Err(); err != nil { - return nil, err - } text := s.Text() if text[0] != '#' { parts := strings.Fields(text) @@ -200,6 +197,9 @@ func GetAllSubsystems() ([]string, error) { } } } + if err := s.Err(); err != nil { + return nil, err + } return subsystems, nil } @@ -265,10 +265,6 @@ func parseCgroupFromReader(r io.Reader) (map[string]string, error) { cgroups := make(map[string]string) for s.Scan() { - if err := s.Err(); err != nil { - return nil, err - } - text := s.Text() // from cgroups(7): // /proc/[pid]/cgroup @@ -285,6 +281,10 @@ func parseCgroupFromReader(r io.Reader) (map[string]string, error) { cgroups[subs] = parts[2] } } + if err := s.Err(); err != nil { + return nil, err + } + return cgroups, nil } diff --git a/libcontainer/seccomp/seccomp_linux.go b/libcontainer/seccomp/seccomp_linux.go index ec0ac1c0..518d2c38 100644 --- a/libcontainer/seccomp/seccomp_linux.go +++ b/libcontainer/seccomp/seccomp_linux.go @@ -212,10 +212,6 @@ func parseStatusFile(path string) (map[string]string, error) { status := make(map[string]string) for s.Scan() { - if err := s.Err(); err != nil { - return nil, err - } - text := s.Text() parts := strings.Split(text, ":") @@ -225,5 +221,9 @@ func parseStatusFile(path string) (map[string]string, error) { status[parts[0]] = parts[1] } + if err := s.Err(); err != nil { + return nil, err + } + return status, nil }