From: Simon Ruderich Date: Wed, 12 Jun 2019 06:45:49 +0000 (+0200) Subject: nsscash: report error for passwd/group without trailing newline X-Git-Tag: 0.1~59 X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=bfa96c6bded3a8a4edab666f12429b0659e30ad2;p=nsscash%2Fnsscash.git nsscash: report error for passwd/group without trailing newline Previously this case was hidden because ReadString() can return a string and an EOF error. To prevent this issue from going unnoticed error out. --- diff --git a/group.go b/group.go index 4ca213a..b96f6b1 100644 --- a/group.go +++ b/group.go @@ -68,6 +68,11 @@ func ParseGroups(r io.Reader) ([]Group, error) { t, err := s.ReadString('\n') if err != nil { if err == io.EOF { + if t != "" { + return nil, fmt.Errorf( + "no newline in last line: %q", + t) + } break } return nil, err diff --git a/group_test.go b/group_test.go index bc91eb3..d5509b9 100644 --- a/group_test.go +++ b/group_test.go @@ -16,6 +16,7 @@ package main import ( + "fmt" "reflect" "strings" "testing" @@ -25,10 +26,17 @@ func TestParseGroups(t *testing.T) { tests := []struct { data string exp []Group + err error }{ { "", nil, + nil, + }, + { + "root:x:0:", + nil, + fmt.Errorf("no newline in last line: \"root:x:0:\""), }, { "root:x:0:\n", @@ -40,6 +48,7 @@ func TestParseGroups(t *testing.T) { Members: nil, }, }, + nil, }, { "root:x:0:foo\n", @@ -53,6 +62,7 @@ func TestParseGroups(t *testing.T) { }, }, }, + nil, }, { "root:x:0:foo,bar\n", @@ -67,14 +77,15 @@ func TestParseGroups(t *testing.T) { }, }, }, + nil, }, } for n, tc := range tests { res, err := ParseGroups(strings.NewReader(tc.data)) - if err != nil { + if !reflect.DeepEqual(err, tc.err) { t.Errorf("%d: err = %v, want %v", - n, res, nil) + n, err, tc.err) } if !reflect.DeepEqual(res, tc.exp) { t.Errorf("%d: res = %v, want %v", diff --git a/passwd.go b/passwd.go index c9f4409..7364639 100644 --- a/passwd.go +++ b/passwd.go @@ -54,6 +54,11 @@ func ParsePasswds(r io.Reader) ([]Passwd, error) { t, err := s.ReadString('\n') if err != nil { if err == io.EOF { + if t != "" { + return nil, fmt.Errorf( + "no newline in last line: %q", + t) + } break } return nil, err