From: Simon Ruderich Date: Tue, 11 Jun 2019 08:56:44 +0000 (+0200) Subject: nsscash: handle errors in SerializePasswd(), SerializeGroup() X-Git-Tag: 0.1~62 X-Git-Url: https://ruderich.org/simon/gitweb/?p=nsscash%2Fnsscash.git;a=commitdiff_plain;h=7adc7f83eddd37aa372aa10159128b6d16c4320a nsscash: handle errors in SerializePasswd(), SerializeGroup() No such errors are defined yet. --- diff --git a/group.go b/group.go index 214f914..126ebb0 100644 --- a/group.go +++ b/group.go @@ -102,7 +102,7 @@ func ParseGroups(r io.Reader) ([]Group, error) { return res, nil } -func SerializeGroup(g Group) []byte { +func SerializeGroup(g Group) ([]byte, error) { le := binary.LittleEndian // Concatenate all (NUL-terminated) strings and store the offsets @@ -158,7 +158,7 @@ func SerializeGroup(g Group) []byte { // struct are 8 byte aligned alignBufferTo(&res, 8) - return res.Bytes() + return res.Bytes(), nil } func SerializeGroups(w io.Writer, grs []Group) error { @@ -168,7 +168,11 @@ func SerializeGroups(w io.Writer, grs []Group) error { for _, g := range grs { // TODO: warn about duplicate entries offsets[toKey(g)] = uint64(data.Len()) - data.Write(SerializeGroup(g)) + x, err := SerializeGroup(g) + if err != nil { + return err + } + data.Write(x) } // Copy to prevent sorting from modifying the argument diff --git a/passwd.go b/passwd.go index 2698765..54da63b 100644 --- a/passwd.go +++ b/passwd.go @@ -86,7 +86,7 @@ func ParsePasswds(r io.Reader) ([]Passwd, error) { return res, nil } -func SerializePasswd(p Passwd) []byte { +func SerializePasswd(p Passwd) ([]byte, error) { // Concatenate all (NUL-terminated) strings and store the offsets var data bytes.Buffer data.Write([]byte(p.Name)) @@ -138,7 +138,7 @@ func SerializePasswd(p Passwd) []byte { // struct are 8 byte aligned alignBufferTo(&res, 8) - return res.Bytes() + return res.Bytes(), nil } func SerializePasswds(w io.Writer, pws []Passwd) error { @@ -148,7 +148,11 @@ func SerializePasswds(w io.Writer, pws []Passwd) error { for _, p := range pws { // TODO: warn about duplicate entries offsets[p] = uint64(data.Len()) - data.Write(SerializePasswd(p)) + x, err := SerializePasswd(p) + if err != nil { + return err + } + data.Write(x) } // Copy to prevent sorting from modifying the argument