Add test/fix for ErrShortBuffer edgecase

pull/6/head
Izaak Alpert 2018-01-25 11:07:56 -05:00 committed by Izaak Alpert (karlhungus)
parent 068e1642f6
commit 6100c7fe3f
No known key found for this signature in database
GPG Key ID: 02C0625B8103869A
2 changed files with 32 additions and 2 deletions

View File

@ -126,8 +126,8 @@ func (d *YAMLDecoder) Read(data []byte) (n int, err error) {
}
// caller will need to reread
copy(data, d.remaining[:left])
d.remaining = d.remaining[left:]
copy(data, d.remaining[:len(data)])
d.remaining = d.remaining[len(data):]
return len(data), io.ErrShortBuffer
}

View File

@ -54,6 +54,36 @@ stuff: 1
}
}
func TestYAMLDecoderCallsAfterErrShortBufferRestOfFrame(t *testing.T) {
d := `---
stuff: 1
test-foo: 1`
r := NewDocumentDecoder(ioutil.NopCloser(bytes.NewReader([]byte(d))))
b := make([]byte, 12)
n, err := r.Read(b)
if err != io.ErrShortBuffer || n != 12 {
t.Fatalf("expected ErrShortBuffer: %d / %v", n, err)
}
expected := "---\nstuff: 1"
if string(b) != expected {
t.Fatalf("expected bytes read to be: %s got: %s", expected, string(b))
}
b = make([]byte, 13)
n, err = r.Read(b)
if err != nil || n != 13 {
t.Fatalf("expected nil: %d / %v", n, err)
}
expected = "\n\ttest-foo: 1"
if string(b) != expected {
t.Fatalf("expected bytes read to be: '%s' got: '%s'", expected, string(b))
}
b = make([]byte, 15)
n, err = r.Read(b)
if err != io.EOF || n != 0 {
t.Fatalf("expected EOF: %d / %v", n, err)
}
}
func TestSplitYAMLDocument(t *testing.T) {
testCases := []struct {
input string