Skip to content

Commit 5f460da

Browse files
committed
mountinfo: GetMountsFromReader() remove redundant switch, fix dropped fields
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 f494bef commit 5f460da

File tree

2 files changed

+3
-10
lines changed

2 files changed

+3
-10
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)

mountinfo/mountinfo_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ func TestParseMountinfoExtraCases(t *testing.T) {
655655
name: "extra optional fields", // which we carefully gather
656656
entry: `251 15 0:3573 / /mnt/point rw,relatime shared:123 extra:tag what:ever key:value - aufs none rw`,
657657
valid: true,
658-
exp: &Info{Mountpoint: "/mnt/point", FSType: "aufs", Source: "none", Optional: "shared:123 extra:tag what:ever"},
658+
exp: &Info{Mountpoint: "/mnt/point", FSType: "aufs", Source: "none", Optional: "shared:123 extra:tag what:ever key:value"},
659659
},
660660
{
661661
name: "empty source field (kernel < 5.1 bug)",

0 commit comments

Comments
 (0)