]> ruderich.org/simon Gitweb - nsscash/nsscash.git/commitdiff
nsscash: report error for passwd/group without trailing newline
authorSimon Ruderich <simon@ruderich.org>
Wed, 12 Jun 2019 06:45:49 +0000 (08:45 +0200)
committerSimon Ruderich <simon@ruderich.org>
Wed, 12 Jun 2019 06:45:49 +0000 (08:45 +0200)
Previously this case was hidden because ReadString() can return a string
and an EOF error. To prevent this issue from going unnoticed error out.

group.go
group_test.go
passwd.go

index 4ca213a0edfabc48fd7a95e4280509c4547c9d5c..b96f6b14aa4c779cc59c1ebc4498c96a5fefcdb2 100644 (file)
--- 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
index bc91eb3c3fe5838c25391f0276ae8b55baf539be..d5509b9a57695353e78c907a79d8d0058e48607a 100644 (file)
@@ -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",
index c9f4409c2bbb013f99631d251389f2e8d2e85de9..7364639d8e2c3581fe71030b85fac22cfee58456 100644 (file)
--- 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