Check for no state from the io.Reader
Read state would assume that having a reader meant there should be a valid state. Check for an empty file and return ErrNoState to differentiate a bad file from an empty one.
This commit is contained in:
parent
10f6d7f30f
commit
f20485550a
|
@ -4,6 +4,7 @@ import (
|
|||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
@ -1800,10 +1801,16 @@ func testForV0State(buf *bufio.Reader) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// ErrNoState is returned by ReadState when the io.Reader contains no data
|
||||
var ErrNoState = errors.New("no state")
|
||||
|
||||
// ReadState reads a state structure out of a reader in the format that
|
||||
// was written by WriteState.
|
||||
func ReadState(src io.Reader) (*State, error) {
|
||||
buf := bufio.NewReader(src)
|
||||
if _, err := buf.Peek(1); err == io.EOF {
|
||||
return nil, ErrNoState
|
||||
}
|
||||
|
||||
if err := testForV0State(buf); err != nil {
|
||||
return nil, err
|
||||
|
|
Loading…
Reference in New Issue