diff --git a/entity.go b/entity.go index a0858e5..b52e08f 100644 --- a/entity.go +++ b/entity.go @@ -141,6 +141,9 @@ func ReadWithOptions(r io.Reader, opts *ReadOptions) (*Entity, error) { if err != nil { return nil, err } + if !h.Valid() { + return nil, nil + } lr.N = math.MaxInt64 diff --git a/textproto/header.go b/textproto/header.go index 10c04f3..eace386 100644 --- a/textproto/header.go +++ b/textproto/header.go @@ -226,6 +226,11 @@ func (h *Header) Len() int { return len(h.l) } +// Valid returns if the data structure is valid, if it was initialized correctly +func (h *Header) Valid() bool { + return h.l != nil && h.m != nil +} + // Map returns all header fields as a map. // // This function is provided for interoperability with the standard library. @@ -533,6 +538,10 @@ func ReadHeader(r *bufio.Reader) (Header, error) { for { kv, err := readContinuedLineSlice(r) + if kv == nil && len(fs) == 0 { + // Construct an invalid header. + return Header{}, err + } if len(kv) == 0 { return newHeader(fs), err }