mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
Merge pull request #131708 from tigrato/automated-cherry-pick-of-#131702-upstream-release-1.33
Automated cherry pick of #131702: Panic in `NewYAMLToJSONDecoder`
This commit is contained in:
commit
b5a1738ccc
@ -247,10 +247,12 @@ func splitYAMLDocument(data []byte, atEOF bool) (advance int, token []byte, err
|
|||||||
// finding a non-YAML-delimited series of objects), it will not switch to YAML.
|
// finding a non-YAML-delimited series of objects), it will not switch to YAML.
|
||||||
// Once it switches to YAML it will not switch back to JSON.
|
// Once it switches to YAML it will not switch back to JSON.
|
||||||
type YAMLOrJSONDecoder struct {
|
type YAMLOrJSONDecoder struct {
|
||||||
json *json.Decoder
|
json *json.Decoder
|
||||||
yaml *YAMLToJSONDecoder
|
jsonConsumed int64 // of the stream total, how much was JSON?
|
||||||
stream *StreamReader
|
yaml *YAMLToJSONDecoder
|
||||||
count int // how many objects have been decoded
|
yamlConsumed int64 // of the stream total, how much was YAML?
|
||||||
|
stream *StreamReader
|
||||||
|
count int // how many objects have been decoded
|
||||||
}
|
}
|
||||||
|
|
||||||
type JSONSyntaxError struct {
|
type JSONSyntaxError struct {
|
||||||
@ -299,8 +301,10 @@ func (d *YAMLOrJSONDecoder) Decode(into interface{}) error {
|
|||||||
if d.json != nil {
|
if d.json != nil {
|
||||||
err := d.json.Decode(into)
|
err := d.json.Decode(into)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
d.stream.Consume(int(d.json.InputOffset()) - d.stream.Consumed())
|
|
||||||
d.count++
|
d.count++
|
||||||
|
consumed := d.json.InputOffset() - d.jsonConsumed
|
||||||
|
d.stream.Consume(int(consumed))
|
||||||
|
d.jsonConsumed += consumed
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err == io.EOF { //nolint:errorlint
|
if err == io.EOF { //nolint:errorlint
|
||||||
@ -334,7 +338,9 @@ func (d *YAMLOrJSONDecoder) Decode(into interface{}) error {
|
|||||||
if d.yaml != nil {
|
if d.yaml != nil {
|
||||||
err := d.yaml.Decode(into)
|
err := d.yaml.Decode(into)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
d.stream.Consume(d.yaml.InputOffset() - d.stream.Consumed())
|
consumed := int64(d.yaml.InputOffset()) - d.yamlConsumed
|
||||||
|
d.stream.Consume(int(consumed))
|
||||||
|
d.yamlConsumed += consumed
|
||||||
d.count++
|
d.count++
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -375,6 +381,7 @@ func (d *YAMLOrJSONDecoder) consumeWhitespace() error {
|
|||||||
if err == io.EOF { //nolint:errorlint
|
if err == io.EOF { //nolint:errorlint
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
consumed += sz
|
||||||
}
|
}
|
||||||
return io.EOF
|
return io.EOF
|
||||||
}
|
}
|
||||||
|
@ -343,6 +343,17 @@ func TestYAMLOrJSONDecoder(t *testing.T) {
|
|||||||
{"foo": "bar"},
|
{"foo": "bar"},
|
||||||
{"baz": "biz"},
|
{"baz": "biz"},
|
||||||
}},
|
}},
|
||||||
|
// First document is JSON, second is YAML but with smaller size.
|
||||||
|
{"{\"foo\": \"bar\"}\n---\na: b", 100, false, false, []generic{
|
||||||
|
{"foo": "bar"},
|
||||||
|
{"a": "b"},
|
||||||
|
}},
|
||||||
|
// First document is JSON, second is YAML,but with smaller size and
|
||||||
|
// trailing whitespace.
|
||||||
|
{"{\"foo\": \"bar\"} \n---\na: b", 100, false, false, []generic{
|
||||||
|
{"foo": "bar"},
|
||||||
|
{"a": "b"},
|
||||||
|
}},
|
||||||
// First document is JSON, second is YAML, longer than the buffer
|
// First document is JSON, second is YAML, longer than the buffer
|
||||||
{"{\"foo\": \"bar\"}\n---\n{baz: biz0123456780123456780123456780123456780123456789}", 20, false, false, []generic{
|
{"{\"foo\": \"bar\"}\n---\n{baz: biz0123456780123456780123456780123456780123456789}", 20, false, false, []generic{
|
||||||
{"foo": "bar"},
|
{"foo": "bar"},
|
||||||
|
Loading…
Reference in New Issue
Block a user