Merge pull request #58817 from karlhungus/bugfix_yaml_decoder_short_buf

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Add test/fix for ErrShortBuffer edgecase

**What this PR does / why we need it**:

Found a bug with YAMLToJSONDecoder where subsequent reads after `io.ErrShortBuffer` would return values from the next yaml section, rather than the rest of the section I was reading.

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #59055 

https://github.com/kubernetes/kubernetes/issues/59055

**Special notes for your reviewer**:

**Release note**:

```release-note
YAMLDecoder Read now tracks rest of buffer on io.ErrShortBuffer
```
pull/6/head
Kubernetes Submit Queue 2018-01-31 00:18:03 -08:00 committed by GitHub
commit f3942e7ded
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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