From 8fb8b2cffc54e4e724dffb1afb45219152c49fda Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 19 Mar 2018 18:15:54 -0400 Subject: [PATCH] make sure ReadState returns an error ReadState would hide any errors, assuming that it was an empty state. This can mask errors on Windows, where the OS enforces read locks on the state file. --- terraform/state.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/terraform/state.go b/terraform/state.go index 89203bbfe..04b14a659 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -9,6 +9,7 @@ import ( "io" "io/ioutil" "log" + "os" "reflect" "sort" "strconv" @@ -1876,13 +1877,21 @@ 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 != nil { - // the error is either io.EOF or "invalid argument", and both are from - // an empty state. + // check for a nil file specifically, since that produces a platform + // specific error if we try to use it in a bufio.Reader. + if f, ok := src.(*os.File); ok && f == nil { return nil, ErrNoState } + buf := bufio.NewReader(src) + + if _, err := buf.Peek(1); err != nil { + if err == io.EOF { + return nil, ErrNoState + } + return nil, err + } + if err := testForV0State(buf); err != nil { return nil, err }