Skip to content

Commit f3942e7

Browse files
author
Kubernetes Submit Queue
authored
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 #59055 **Special notes for your reviewer**: **Release note**: ```release-note YAMLDecoder Read now tracks rest of buffer on io.ErrShortBuffer ```
2 parents 494664a + 6100c7f commit f3942e7

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ func (d *YAMLDecoder) Read(data []byte) (n int, err error) {
126126
}
127127

128128
// caller will need to reread
129-
copy(data, d.remaining[:left])
130-
d.remaining = d.remaining[left:]
129+
copy(data, d.remaining[:len(data)])
130+
d.remaining = d.remaining[len(data):]
131131
return len(data), io.ErrShortBuffer
132132
}
133133

staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,36 @@ stuff: 1
5454
}
5555
}
5656

57+
func TestYAMLDecoderCallsAfterErrShortBufferRestOfFrame(t *testing.T) {
58+
d := `---
59+
stuff: 1
60+
test-foo: 1`
61+
r := NewDocumentDecoder(ioutil.NopCloser(bytes.NewReader([]byte(d))))
62+
b := make([]byte, 12)
63+
n, err := r.Read(b)
64+
if err != io.ErrShortBuffer || n != 12 {
65+
t.Fatalf("expected ErrShortBuffer: %d / %v", n, err)
66+
}
67+
expected := "---\nstuff: 1"
68+
if string(b) != expected {
69+
t.Fatalf("expected bytes read to be: %s got: %s", expected, string(b))
70+
}
71+
b = make([]byte, 13)
72+
n, err = r.Read(b)
73+
if err != nil || n != 13 {
74+
t.Fatalf("expected nil: %d / %v", n, err)
75+
}
76+
expected = "\n\ttest-foo: 1"
77+
if string(b) != expected {
78+
t.Fatalf("expected bytes read to be: '%s' got: '%s'", expected, string(b))
79+
}
80+
b = make([]byte, 15)
81+
n, err = r.Read(b)
82+
if err != io.EOF || n != 0 {
83+
t.Fatalf("expected EOF: %d / %v", n, err)
84+
}
85+
}
86+
5787
func TestSplitYAMLDocument(t *testing.T) {
5888
testCases := []struct {
5989
input string

0 commit comments

Comments
 (0)