Incorporate PR review comments.

This commit is contained in:
Toby Burress 2018-02-22 17:37:10 +00:00
parent cdb48a8970
commit 08a5281bd4
1 changed files with 18 additions and 19 deletions

View File

@ -183,12 +183,13 @@ const (
eagerEntries = 15 eagerEntries = 15
) )
// readRecords reads count records from the underlying ReaderAt, returning the // readRecords reads up to max records from the underlying ReaderAt, returning
// raw header, the total number of records in the header, and any error. If // the raw header, the total number of records in the header, and any error.
// the header contains fewer than count entries, the return value is truncated. // If the header contains fewer than max entries, the header is truncated to
func readRecords(rd io.ReaderAt, size int64, count int) ([]byte, int, error) { // the appropriate size.
func readRecords(rd io.ReaderAt, size int64, max int) ([]byte, int, error) {
var bufsize int var bufsize int
bufsize += count * int(entrySize) bufsize += max * int(entrySize)
bufsize += crypto.Extension bufsize += crypto.Extension
bufsize += headerLengthSize bufsize += headerLengthSize
@ -202,36 +203,34 @@ func readRecords(rd io.ReaderAt, size int64, count int) ([]byte, int, error) {
return nil, 0, err return nil, 0, err
} }
header := b[len(b)-headerLengthSize:] hlen := binary.LittleEndian.Uint32(b[len(b)-headerLengthSize:])
b = b[:len(b)-headerLengthSize] b = b[:len(b)-headerLengthSize]
hl := binary.LittleEndian.Uint32(header) debug.Log("header length: %v", hlen)
debug.Log("header length: %v", hl)
var err error var err error
switch { switch {
case hl == 0: case hlen == 0:
err = InvalidFileError{Message: "header length is zero"} err = InvalidFileError{Message: "header length is zero"}
case hl < crypto.Extension: case hlen < crypto.Extension:
err = InvalidFileError{Message: "header length is too small"} err = InvalidFileError{Message: "header length is too small"}
case (hl-crypto.Extension)%uint32(entrySize) != 0: case (hlen-crypto.Extension)%uint32(entrySize) != 0:
err = InvalidFileError{Message: "header length is invalid"} err = InvalidFileError{Message: "header length is invalid"}
case int64(hl) > size-int64(headerLengthSize): case int64(hlen) > size-int64(headerLengthSize):
err = InvalidFileError{Message: "header is larger than file"} err = InvalidFileError{Message: "header is larger than file"}
case int64(hl) > maxHeaderSize: case int64(hlen) > maxHeaderSize:
err = InvalidFileError{Message: "header is larger than maxHeaderSize"} err = InvalidFileError{Message: "header is larger than maxHeaderSize"}
} }
if err != nil { if err != nil {
return nil, 0, errors.Wrap(err, "readHeader") return nil, 0, errors.Wrap(err, "readHeader")
} }
c := (int(hl) - crypto.Extension) / int(entrySize) total := (int(hlen) - crypto.Extension) / int(entrySize)
if c < count { if total < max {
recordSize := c * int(entrySize) // truncate to the beginning of the pack header
start := len(b) - (recordSize + crypto.Extension) b = b[len(b)-int(hlen):]
b = b[start:]
} }
return b, c, nil return b, total, nil
} }
// readHeader reads the header at the end of rd. size is the length of the // readHeader reads the header at the end of rd. size is the length of the