]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blobdiff - passwd.go
nss: Makefile: don't link against asan
[nsscash/nsscash.git] / passwd.go
index 2698765212e0975738ebc8d8273140daee528816..c9f4409c2bbb013f99631d251389f2e8d2e85de9 100644 (file)
--- a/passwd.go
+++ b/passwd.go
@@ -23,6 +23,7 @@ import (
        "encoding/binary"
        "fmt"
        "io"
+       "math"
        "sort"
        "strconv"
        "strings"
@@ -86,7 +87,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))
@@ -103,6 +104,11 @@ func SerializePasswd(p Passwd) []byte {
        offShell := uint16(data.Len())
        data.Write([]byte(p.Shell))
        data.WriteByte(0)
+       // Ensure the offsets can fit the length of this entry
+       if data.Len() > math.MaxUint16 {
+               return nil, fmt.Errorf("passwd too large to serialize: %v, %v",
+                       data.Len(), p)
+       }
        size := uint16(data.Len())
 
        var res bytes.Buffer // serialized result
@@ -138,7 +144,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 +154,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