X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=passwd.go;h=6da64762ad2dae302dd9e8c6c097bec08e0a46fb;hb=refs%2Fheads%2Fmaster;hp=54da63b3f30252f65e5c1717318b683686a037c1;hpb=7adc7f83eddd37aa372aa10159128b6d16c4320a;p=nsscash%2Fnsscash.git diff --git a/passwd.go b/passwd.go index 54da63b..6da6476 100644 --- a/passwd.go +++ b/passwd.go @@ -1,6 +1,6 @@ // Parse /etc/passwd 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" @@ -53,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 @@ -103,6 +109,11 @@ func SerializePasswd(p Passwd) ([]byte, error) { 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