Skip to content

Commit

Permalink
Exercise both in-memory BufReader and file-backed FileReader in Decbu…
Browse files Browse the repository at this point in the history
…f tests.

This revealed two differences in behaviour between BufReader and
FileReader:
* creating a FileReader for an empty file would fail with an error,
  whereas creating a BufReader for an empty buffer would succeed (and
  subsequent reads would fail with an 'invalid size' error)
* reading from a FileReader that had reached the end of the file would
  return a nil byte slice, whereas reading from a BufReader that had
  reached the end of its buffer would return an empty byte slice

Now, both implementations will fail at creation if given an empty
source file / buffer, and both will return an empty slice if reading
from the end of the source file / buffer.
  • Loading branch information
charleskorn committed Nov 29, 2022
1 parent 01d09fe commit 685926f
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 189 deletions.
16 changes: 14 additions & 2 deletions pkg/storegateway/indexheader/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ type Decbuf struct {
}

func NewDecbufAt(bs ByteSlice, off int, castagnoliTable *crc32.Table) Decbuf {
return NewDecbuf(NewBufReader(bs), off, castagnoliTable)
reader, err := NewBufReader(bs)

if err != nil {
return Decbuf{E: err}
}

return NewDecbuf(reader, off, castagnoliTable)
}

// NewDecbufAt returns a new decoding buffer. It expects the first 4 bytes
Expand Down Expand Up @@ -137,7 +143,13 @@ func NewDecbufUvarintAt(bs ByteSlice, off int, castagnoliTable *crc32.Table) Dec
}
*/
func NewDecbufRaw(bs ByteSlice) Decbuf {
return Decbuf{r: NewBufReader(bs)}
reader, err := NewBufReader(bs)

if err != nil {
return Decbuf{E: err}
}

return Decbuf{r: reader}
}

func NewDecbufRawReader(r Reader) Decbuf {
Expand Down
Loading

0 comments on commit 685926f

Please sign in to comment.