Skip to content

Commit 706f6e5

Browse files
committed
user: skip malformed lines
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 266649b commit 706f6e5

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

user/user.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func parseLine(line []byte, v ...any) error {
6262

6363
func parseParts(parts [][]byte, v ...any) error {
6464
if len(parts) == 0 {
65+
// TODO(thaJeztah); do we need an "ok" return so that we can skip these in loops?
6566
return nil
6667
}
6768

@@ -151,7 +152,9 @@ func ParsePasswdFilter(r io.Reader, filter func(User) bool) ([]User, error) {
151152
// adm:x:3:4:adm:/var/adm:/bin/false
152153
p := User{}
153154
if err := parseLine(line, &p.Name, &p.Pass, &p.Uid, &p.Gid, &p.Gecos, &p.Home, &p.Shell); err != nil {
154-
return nil, err
155+
// Skip malformed lines, and don't consider them.
156+
// return nil, fmt.Errorf("invalid line (%q): %w", string(line), err)
157+
continue
155158
}
156159

157160
if filter == nil || filter(p) {
@@ -212,7 +215,9 @@ func ParseGroupFilter(r io.Reader, filter func(Group) bool) ([]Group, error) {
212215
// adm:x:4:root,adm,daemon
213216
p := Group{}
214217
if err := parseLine(line, &p.Name, &p.Pass, &p.Gid, &p.List); err != nil {
215-
return nil, err
218+
// Skip malformed lines, and don't consider them.
219+
// return nil, fmt.Errorf("invalid line (%q): %w", string(line), err)
220+
continue
216221
}
217222

218223
if filter == nil || filter(p) {
@@ -528,6 +533,8 @@ func ParseSubIDFilter(r io.Reader, filter func(SubID) bool) ([]SubID, error) {
528533
// see: man 5 subuid
529534
p := SubID{}
530535
if err := parseLine(line, &p.Name, &p.SubID, &p.Count); err != nil {
536+
// Skip malformed lines, and don't consider them.
537+
// return nil, fmt.Errorf("invalid line (%q): %w", string(line), err)
531538
return nil, err
532539
}
533540

@@ -578,7 +585,9 @@ func ParseIDMapFilter(r io.Reader, filter func(IDMap) bool) ([]IDMap, error) {
578585
// see: man 7 user_namespaces
579586
p := IDMap{}
580587
if err := parseParts(bytes.Fields(line), &p.ID, &p.ParentID, &p.Count); err != nil {
581-
return nil, err
588+
// Skip malformed lines, and don't consider them.
589+
// return nil, fmt.Errorf("invalid line (%q): %w", string(line), err)
590+
continue
582591
}
583592

584593
if filter == nil || filter(p) {

0 commit comments

Comments
 (0)