X-Git-Url: https://ruderich.org/simon/gitweb/?p=nsscash%2Fnsscash.git;a=blobdiff_plain;f=group.go;h=018a0c32716c665c690c78903b6eccdb5435ac54;hp=126ebb0dc349e0127c4ff14f3b61f48cb99cc737;hb=HEAD;hpb=7adc7f83eddd37aa372aa10159128b6d16c4320a diff --git a/group.go b/group.go index 126ebb0..018a0c3 100644 --- a/group.go +++ b/group.go @@ -1,6 +1,6 @@ // Parse /etc/group files and serialize them -// Copyright (C) 2019 Simon Ruderich +// Copyright (C) 2019-2021 Simon Ruderich // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by @@ -23,6 +23,7 @@ import ( "encoding/binary" "fmt" "io" + "math" "sort" "strconv" "strings" @@ -67,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 @@ -130,6 +136,11 @@ func SerializeGroup(g Group) ([]byte, error) { } // 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 @@ -183,7 +194,7 @@ func SerializeGroups(w io.Writer, grs []Group) error { tmp := make([]byte, 8) // Create index "sorted" in input order, used when iterating over all - // passwd entries (getgrent_r); keeping the original order makes + // group entries (getgrent_r); keeping the original order makes // debugging easier var indexOrig bytes.Buffer for _, g := range grs {