]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blobdiff - group.go
nss: Makefile: don't link against asan
[nsscash/nsscash.git] / group.go
index 214f91438d78740a331c487cbfe5ecab1355718c..4ca213a0edfabc48fd7a95e4280509c4547c9d5c 100644 (file)
--- a/group.go
+++ b/group.go
@@ -26,6 +26,7 @@ import (
        "sort"
        "strconv"
        "strings"
+       "math"
 
        "github.com/pkg/errors"
 )
@@ -102,7 +103,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
@@ -130,6 +131,11 @@ func SerializeGroup(g Group) []byte {
        }
        // And the group members concatenated as above
        data.Write(mems.Bytes())
+       // Ensure the offsets can fit the length of this entry
+       if data.Len() > math.MaxUint16 {
+               return nil, fmt.Errorf("group too large to serialize: %v, %v",
+                       data.Len(), g)
+       }
        size := uint16(data.Len())
 
        var res bytes.Buffer // serialized result
@@ -158,7 +164,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 +174,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