Skip to content

Commit f791ef1

Browse files
committed
mountinfo: GetMountsFromReader() remove redundant switch
This switch was implementing the exact same check as is done in strings.Join(); https://cs.opensource.google/go/go/+/refs/tags/go1.16.7:src/strings/strings.go;l=421-427 This only difference removing this switch makes is that it will assign an empty string to p.Optional if no optional fields are present, but given that an empty string is the default value for p.Optional, this should make no difference. This also revealed an off-by-one bug: Before this change: - we return an error if `sepIdx == 5` (earlier check) - for `sepIdx == 6` we skipped (in this switch) - for `7`, we take field 6 (equivalent to `fields[6:7]`, or `fields[6:sepIdx]`) - for the "default" (else), we use `fields[6:sepIdx-1]`, which for (e.g.) `sepIndex=8` would do the same as `sepIdx=7`, so we'd drop one value. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent f3885c8 commit f791ef1

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

mountinfo/mountinfo_linux.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,9 @@ func GetMountsFromReader(r io.Reader, filter FilterFunc) ([]*Info, error) {
105105
p.Options = fields[5]
106106

107107
// zero or more optional fields
108-
switch {
109-
case sepIdx == 6:
110-
// zero, do nothing
111-
case sepIdx == 7:
112-
p.Optional = fields[6]
113-
default:
114-
p.Optional = strings.Join(fields[6:sepIdx-1], " ")
115-
}
108+
p.Optional = strings.Join(fields[6:sepIdx], " ")
116109

117-
// Run the filter after parsing all of the fields.
110+
// Run the filter after parsing all fields.
118111
var skip, stop bool
119112
if filter != nil {
120113
skip, stop = filter(p)

0 commit comments

Comments
 (0)