From 71126e79d1056b22efc715e53d484dbaae9ee868 Mon Sep 17 00:00:00 2001 From: Daniel Smith Date: Thu, 16 Oct 2014 13:38:18 -0700 Subject: [PATCH] Don't accept empty input in DecodeInto --- pkg/conversion/decode.go | 7 +++++++ pkg/conversion/scheme_test.go | 3 +++ 2 files changed, 10 insertions(+) diff --git a/pkg/conversion/decode.go b/pkg/conversion/decode.go index 23a4a119634..5f2f8b0404f 100644 --- a/pkg/conversion/decode.go +++ b/pkg/conversion/decode.go @@ -17,6 +17,7 @@ limitations under the License. package conversion import ( + "errors" "fmt" "gopkg.in/v1/yaml" @@ -73,6 +74,12 @@ func (s *Scheme) Decode(data []byte) (interface{}, error) { // If obj's version doesn't match that in data, an attempt will be made to convert // data into obj's version. func (s *Scheme) DecodeInto(data []byte, obj interface{}) error { + if len(data) == 0 { + // This is valid YAML, but it's a bad idea not to return an error + // for an empty string-- that's almost certainly not what the caller + // was expecting. + return errors.New("empty input") + } dataVersion, dataKind, err := s.DataVersionAndKind(data) if err != nil { return err diff --git a/pkg/conversion/scheme_test.go b/pkg/conversion/scheme_test.go index b299299e545..81000d390ea 100644 --- a/pkg/conversion/scheme_test.go +++ b/pkg/conversion/scheme_test.go @@ -290,6 +290,9 @@ func TestBadJSONRejection(t *testing.T) { if err := s.DecodeInto(badJSONKindMismatch, &TestType1{}); err == nil { t.Errorf("Kind is set but doesn't match the object type: %s", badJSONKindMismatch) } + if err := s.DecodeInto([]byte(``), &TestType1{}); err == nil { + t.Errorf("Did not give error for empty data") + } } func TestBadJSONRejectionForSetInternalVersion(t *testing.T) {