Skip to content

pkg/record: Decode should return ErrUnexpectedEOF instead of EOF when header is read but expected data is missing #73

Description

@dolmen

When a record header is correctly read, but no data follows (io.ReadFull line 86 returns io.EOF), Decode will return io.EOF instead of io.ErrUnexpectedEOF (which would signal a partial broken record instead of no record).

// Decode reads a binary record from an io.Reader and fills the provided
// *Record struct.
//
// It uses a pointer over a *Record struct instead of returning a *Record
// struct for performance reason: it tries to reuse the already allocated Data
// field.
func Decode(rd io.Reader, rec *Record) error {
var hdr Header
if err := binary.Read(rd, binary.BigEndian, &hdr); err != nil {
return err
}
rec.Time = time.Unix(0, int64(hdr.Time))
rec.Fd = int(hdr.Fd)
rec.Size = int(hdr.Size)
// Reuse the data slice
if cap(rec.Data) >= int(rec.Size) {
rec.Data = rec.Data[:rec.Size]
} else {
rec.Data = make([]byte, rec.Size)
}
if _, err := io.ReadFull(rd, rec.Data); err != nil {
return err
}
return nil
}

io.ErrUnexpectedEOF would signal a problem with the file instead of just hiding the issue to the caller.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions