mountinfo: GetMountsFromReader() remove redundant switch, fix dropped "optional" fields#81
Merged
AkihiroSuda merged 3 commits intomoby:masterfrom Aug 23, 2021
Merged
Conversation
thaJeztah
commented
Aug 14, 2021
| case sepIdx == 7: | ||
| p.Optional = fields[6] | ||
| default: | ||
| p.Optional = strings.Join(fields[6:sepIdx-1], " ") |
Member
Author
There was a problem hiding this comment.
Looks like there's an off-by-one bug here; removing this switch caused it to panic;
--- FAIL: TestMountedBy (0.01s)
panic: runtime error: slice bounds out of range [6:5] [recovered]
panic: runtime error: slice bounds out of range [6:5]
- we return an error if
sepIdx == 5(earlier check) - for
sepIdx == 6we skipped (in this switch) - for
7, we take field 6 (equivalent tofields[6:7], orfields[6:sepIdx]) - for the "default" (else), we use
fields[6:sepIdx-1], which for (e.g.)sepIndex=8would do the same assepIdx=7, so we'd drop one value
2f3fda1 to
f791ef1
Compare
Member
Author
|
@kolyshkin PTAL |
f791ef1 to
5f460da
Compare
Member
Author
|
Looking further, I noticed that TestParseMountinfoExtraCases was not checking the |
Member
Author
|
@AkihiroSuda ptal |
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
… 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>
5f460da to
df5b314
Compare
AkihiroSuda
approved these changes
Aug 23, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
sepIdx == 5(earlier check)sepIdx == 6we skipped (in this switch)7, we take field 6 (equivalent tofields[6:7], orfields[6:sepIdx])fields[6:sepIdx-1], which for (e.g.)sepIndex=8would do the same as
sepIdx=7, so we'd drop one value.