Skip to content

Commit 3a71eb0

Browse files
committed
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 <[email protected]>
1 parent a9610f2 commit 3a71eb0

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

libcontainer/cgroups/utils.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,6 @@ func GetAllSubsystems() ([]string, error) {
189189

190190
s := bufio.NewScanner(f)
191191
for s.Scan() {
192-
if err := s.Err(); err != nil {
193-
return nil, err
194-
}
195192
text := s.Text()
196193
if text[0] != '#' {
197194
parts := strings.Fields(text)
@@ -200,6 +197,9 @@ func GetAllSubsystems() ([]string, error) {
200197
}
201198
}
202199
}
200+
if err := s.Err(); err != nil {
201+
return nil, err
202+
}
203203
return subsystems, nil
204204
}
205205

@@ -265,10 +265,6 @@ func parseCgroupFromReader(r io.Reader) (map[string]string, error) {
265265
cgroups := make(map[string]string)
266266

267267
for s.Scan() {
268-
if err := s.Err(); err != nil {
269-
return nil, err
270-
}
271-
272268
text := s.Text()
273269
// from cgroups(7):
274270
// /proc/[pid]/cgroup
@@ -285,6 +281,10 @@ func parseCgroupFromReader(r io.Reader) (map[string]string, error) {
285281
cgroups[subs] = parts[2]
286282
}
287283
}
284+
if err := s.Err(); err != nil {
285+
return nil, err
286+
}
287+
288288
return cgroups, nil
289289
}
290290

libcontainer/seccomp/seccomp_linux.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,6 @@ func parseStatusFile(path string) (map[string]string, error) {
212212
status := make(map[string]string)
213213

214214
for s.Scan() {
215-
if err := s.Err(); err != nil {
216-
return nil, err
217-
}
218-
219215
text := s.Text()
220216
parts := strings.Split(text, ":")
221217

@@ -225,5 +221,9 @@ func parseStatusFile(path string) (map[string]string, error) {
225221

226222
status[parts[0]] = parts[1]
227223
}
224+
if err := s.Err(); err != nil {
225+
return nil, err
226+
}
227+
228228
return status, nil
229229
}

0 commit comments

Comments
 (0)