From c6b898ea9ab372c58c59658c2acf17ea831d71db Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Tue, 11 Jun 2019 10:53:43 +0200 Subject: [PATCH] nsscash: support longer lines in passwd/group files bufio.Scanner has an internal limit on the maximum token (= line) length. We want to support larger lines in the future. --- group.go | 19 ++++++++++++------- passwd.go | 20 +++++++++++--------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/group.go b/group.go index 0ad8671..214f914 100644 --- a/group.go +++ b/group.go @@ -62,9 +62,15 @@ func toKey(g Group) GroupKey { func ParseGroups(r io.Reader) ([]Group, error) { var res []Group - s := bufio.NewScanner(r) - for s.Scan() { - t := s.Text() + s := bufio.NewReader(r) + for { + t, err := s.ReadString('\n') + if err != nil { + if err == io.EOF { + break + } + return nil, err + } x := strings.Split(t, ":") if len(x) != 4 { @@ -76,6 +82,9 @@ func ParseGroups(r io.Reader) ([]Group, error) { return nil, errors.Wrapf(err, "invalid gid in line %q", t) } + // ReadString() contains the delimiter + x[3] = strings.TrimSuffix(x[3], "\n") + var members []string // No members must result in empty slice, not slice with the // empty string @@ -89,10 +98,6 @@ func ParseGroups(r io.Reader) ([]Group, error) { Members: members, }) } - err := s.Err() - if err != nil { - return nil, err - } return res, nil } diff --git a/passwd.go b/passwd.go index 07a62d8..2698765 100644 --- a/passwd.go +++ b/passwd.go @@ -48,9 +48,15 @@ type Passwd struct { func ParsePasswds(r io.Reader) ([]Passwd, error) { var res []Passwd - s := bufio.NewScanner(r) - for s.Scan() { - t := s.Text() + s := bufio.NewReader(r) + for { + t, err := s.ReadString('\n') + if err != nil { + if err == io.EOF { + break + } + return nil, err + } x := strings.Split(t, ":") if len(x) != 7 { @@ -73,14 +79,10 @@ func ParsePasswds(r io.Reader) ([]Passwd, error) { Gid: gid, Gecos: x[4], Dir: x[5], - Shell: x[6], + // ReadString() contains the delimiter + Shell: strings.TrimSuffix(x[6], "\n"), }) } - err := s.Err() - if err != nil { - return nil, err - } - return res, nil } -- 2.45.2