-
Notifications
You must be signed in to change notification settings - Fork 56
[WIP] user: skip malformed and empty lines #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
thaJeztah
wants to merge
4
commits into
moby:main
Choose a base branch
from
thaJeztah:parseparts_err
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+102
−30
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,32 +56,49 @@ type IDMap struct { | |
| Count int64 | ||
| } | ||
|
|
||
| func parseLine(line []byte, v ...any) { | ||
| parseParts(bytes.Split(line, []byte(":")), v...) | ||
| func parseLine(line []byte, v ...any) (ok bool, _ error) { | ||
| line = bytes.TrimSpace(line) | ||
| if len(line) == 0 { | ||
| return false, nil | ||
| } | ||
| return parseParts(bytes.Split(line, []byte(":")), v...) | ||
| } | ||
|
|
||
| func parseParts(parts [][]byte, v ...any) { | ||
| func parseParts(parts [][]byte, fields ...any) (ok bool, _ error) { | ||
| if len(parts) == 0 { | ||
| return | ||
| return false, nil | ||
| } | ||
|
|
||
| for i, p := range parts { | ||
| // Ignore cases where we don't have enough fields to populate the arguments. | ||
| // Some configuration files like to misbehave. | ||
| if len(v) <= i { | ||
| break | ||
| for i, v := range fields { | ||
| var p []byte | ||
| if i < len(parts) { | ||
| // Ignore cases where we don't have enough fields to populate the arguments. | ||
| // Some configuration files like to misbehave. | ||
| // Depending on the field-type, missing fields can be either ignored, | ||
| // (e.g. empty string) or produce an error (missing/empty numeric field). | ||
| p = parts[i] | ||
| } | ||
|
|
||
| // Use the type of the argument to figure out how to parse it, scanf() style. | ||
| // This is legit. | ||
| switch e := v[i].(type) { | ||
| switch e := v.(type) { | ||
| case *string: | ||
| *e = string(p) | ||
| case *int: | ||
| // "numbers", with conversion errors ignored because of some misbehaving configuration files. | ||
| *e, _ = strconv.Atoi(string(p)) | ||
| n, ok, err := parseNumeric(string(p)) | ||
| if err != nil { | ||
| return true, fmt.Errorf("parsing integer field %d: %w", i, err) | ||
| } | ||
| if !ok { | ||
| return true, fmt.Errorf("parsing integer field %d: %q is not numeric", i, p) | ||
| } | ||
| *e = n | ||
| case *int64: | ||
| *e, _ = strconv.ParseInt(string(p), 10, 64) | ||
| n, err := strconv.ParseInt(string(p), 10, 64) | ||
| if err != nil { | ||
| return true, fmt.Errorf("parsing integer field %d: %w", i, err) | ||
| } | ||
| *e = n | ||
| case *[]string: | ||
| // Comma-separated lists. | ||
| if len(p) != 0 { | ||
|
|
@@ -94,6 +111,7 @@ func parseParts(parts [][]byte, v ...any) { | |
| panic(fmt.Sprintf("parseLine only accepts {*string, *int, *int64, *[]string} as arguments! %#v is not a pointer!", e)) | ||
| } | ||
| } | ||
| return true, nil | ||
| } | ||
|
|
||
| func ParsePasswdFile(path string) ([]User, error) { | ||
|
|
@@ -126,6 +144,7 @@ func ParsePasswdFilter(r io.Reader, filter func(User) bool) ([]User, error) { | |
| for s.Scan() { | ||
| line := bytes.TrimSpace(s.Bytes()) | ||
| if len(line) == 0 { | ||
| // Skip empty lines, and don't consider them. | ||
| continue | ||
| } | ||
|
|
||
|
|
@@ -135,7 +154,11 @@ func ParsePasswdFilter(r io.Reader, filter func(User) bool) ([]User, error) { | |
| // root:x:0:0:root:/root:/bin/bash | ||
| // adm:x:3:4:adm:/var/adm:/bin/false | ||
| p := User{} | ||
| parseLine(line, &p.Name, &p.Pass, &p.Uid, &p.Gid, &p.Gecos, &p.Home, &p.Shell) | ||
| ok, err := parseLine(line, &p.Name, &p.Pass, &p.Uid, &p.Gid, &p.Gecos, &p.Home, &p.Shell) | ||
| if err != nil || !ok { | ||
| // Skip malformed and empty lines, and don't consider them. | ||
| continue | ||
| } | ||
|
Comment on lines
+157
to
+161
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly also skip zero-records; if p == (User{}) {
continue
} |
||
|
|
||
| if filter == nil || filter(p) { | ||
| out = append(out, p) | ||
|
|
@@ -194,7 +217,11 @@ func ParseGroupFilter(r io.Reader, filter func(Group) bool) ([]Group, error) { | |
| // root:x:0:root | ||
| // adm:x:4:root,adm,daemon | ||
| p := Group{} | ||
| parseLine(line, &p.Name, &p.Pass, &p.Gid, &p.List) | ||
| ok, err := parseLine(line, &p.Name, &p.Pass, &p.Gid, &p.List) | ||
| if err != nil || !ok { | ||
| // Skip malformed and empty lines, and don't consider them. | ||
| continue | ||
| } | ||
|
|
||
| if filter == nil || filter(p) { | ||
| out = append(out, p) | ||
|
Comment on lines
+220
to
227
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here as well, but |
||
|
|
@@ -501,7 +528,11 @@ func ParseSubIDFilter(r io.Reader, filter func(SubID) bool) ([]SubID, error) { | |
|
|
||
| // see: man 5 subuid | ||
| p := SubID{} | ||
| parseLine(line, &p.Name, &p.SubID, &p.Count) | ||
| ok, err := parseLine(line, &p.Name, &p.SubID, &p.Count) | ||
| if err != nil || !ok { | ||
| // Skip malformed and empty lines, and don't consider them. | ||
| continue | ||
| } | ||
|
|
||
| if filter == nil || filter(p) { | ||
| out = append(out, p) | ||
|
|
@@ -549,7 +580,11 @@ func ParseIDMapFilter(r io.Reader, filter func(IDMap) bool) ([]IDMap, error) { | |
|
|
||
| // see: man 7 user_namespaces | ||
| p := IDMap{} | ||
| parseParts(bytes.Fields(line), &p.ID, &p.ParentID, &p.Count) | ||
| ok, err := parseParts(bytes.Fields(line), &p.ID, &p.ParentID, &p.Count) | ||
| if err != nil || !ok { | ||
| // Skip malformed and empty lines, and don't consider them. | ||
| continue | ||
| } | ||
|
|
||
| if filter == nil || filter(p) { | ||
| out = append(out, p) | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note to self; this should not use
parseNumeric, because it's too permissive. Perhaps we should rename it toparseMaybeNumericto be more clear on intent (it's used for usernames that may be numeric);sys/user/user.go
Lines 245 to 253 in 4a8455d