-
Notifications
You must be signed in to change notification settings - Fork 54
[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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,13 +56,13 @@ type IDMap struct { | |
| Count int64 | ||
| } | ||
|
|
||
| func parseLine(line []byte, v ...any) { | ||
| parseParts(bytes.Split(line, []byte(":")), v...) | ||
| func parseLine(line []byte, v ...any) error { | ||
| return parseParts(bytes.Split(line, []byte(":")), v...) | ||
| } | ||
|
|
||
| func parseParts(parts [][]byte, v ...any) { | ||
| func parseParts(parts [][]byte, v ...any) error { | ||
| if len(parts) == 0 { | ||
| return | ||
| return nil | ||
| } | ||
|
|
||
| for i, p := range parts { | ||
|
|
@@ -78,10 +78,24 @@ func parseParts(parts [][]byte, v ...any) { | |
| case *string: | ||
| *e = string(p) | ||
| case *int: | ||
| // "numbers", with conversion errors ignored because of some misbehaving configuration files. | ||
| *e, _ = strconv.Atoi(string(p)) | ||
| if len(p) != 0 { | ||
| n, ok, err := parseNumeric(string(p)) | ||
| if err != nil { | ||
| return fmt.Errorf("parsing integer field %d: %w", i, err) | ||
| } | ||
| if !ok { | ||
| return fmt.Errorf("parsing integer field %d: %q is not numeric", i, p) | ||
| } | ||
| *e = n | ||
| } | ||
| case *int64: | ||
| *e, _ = strconv.ParseInt(string(p), 10, 64) | ||
| if len(p) != 0 { | ||
| n, err := strconv.ParseInt(string(p), 10, 64) | ||
| if err != nil { | ||
| return fmt.Errorf("parsing integer field %d: %w", i, err) | ||
| } | ||
| *e = n | ||
| } | ||
| case *[]string: | ||
| // Comma-separated lists. | ||
| if len(p) != 0 { | ||
|
|
@@ -94,6 +108,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 nil | ||
| } | ||
|
|
||
| func ParsePasswdFile(path string) ([]User, error) { | ||
|
|
@@ -135,7 +150,9 @@ 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) | ||
| if err := parseLine(line, &p.Name, &p.Pass, &p.Uid, &p.Gid, &p.Gecos, &p.Home, &p.Shell); err != nil { | ||
| return nil, fmt.Errorf("invalid line (%q): %w", string(line), err) | ||
| } | ||
|
Comment on lines
+158
to
+162
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 +211,9 @@ 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) | ||
| if err := parseLine(line, &p.Name, &p.Pass, &p.Gid, &p.List); err != nil { | ||
| return nil, fmt.Errorf("invalid line (%q): %w", string(line), err) | ||
|
Collaborator
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. ditto |
||
| } | ||
|
|
||
| if filter == nil || filter(p) { | ||
| out = append(out, p) | ||
|
Comment on lines
+221
to
228
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 |
||
|
|
@@ -508,7 +527,9 @@ 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) | ||
| if err := parseLine(line, &p.Name, &p.SubID, &p.Count); err != nil { | ||
| return nil, fmt.Errorf("invalid line (%q): %w", string(line), err) | ||
| } | ||
|
|
||
| if filter == nil || filter(p) { | ||
| out = append(out, p) | ||
|
|
@@ -556,7 +577,9 @@ 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) | ||
| if err := parseParts(bytes.Fields(line), &p.ID, &p.ParentID, &p.Count); err != nil { | ||
| return nil, fmt.Errorf("invalid line (%q): %w", string(line), err) | ||
| } | ||
|
|
||
| if filter == nil || filter(p) { | ||
| out = append(out, p) | ||
|
|
||
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.
nit: you have both
()and""aroundline, which seems excessive.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.
Ah, yes, I think I was going back-and-forth on
%qor(%s)-%qisn't always great due to escaping nested quotes if they can be expected.