From cfe7d0424356f2e75660525d100617eae45f09a8 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Sun, 11 May 2025 16:16:46 -0700 Subject: [PATCH] Account consumed newlines properly in YAML decoder This isn't a significant functional bug in that the StreamDecoder is still offset to account for it (which is why we never saw it before). But it's a trap for future use-cases. Also move one LOC to match a parallel-shaped block above. --- staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder.go b/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder.go index bd91a189e65..66bf31eea17 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder.go @@ -338,10 +338,10 @@ func (d *YAMLOrJSONDecoder) Decode(into interface{}) error { if d.yaml != nil { err := d.yaml.Decode(into) if err == nil { + d.count++ consumed := int64(d.yaml.InputOffset()) - d.yamlConsumed d.stream.Consume(int(consumed)) d.yamlConsumed += consumed - d.count++ return nil } if err == io.EOF { //nolint:errorlint @@ -374,6 +374,7 @@ func (d *YAMLOrJSONDecoder) consumeWhitespace() error { d.stream.Consume(consumed) return nil } + consumed += sz if r == '\n' { d.stream.Consume(consumed) return nil @@ -381,7 +382,6 @@ func (d *YAMLOrJSONDecoder) consumeWhitespace() error { if err == io.EOF { //nolint:errorlint break } - consumed += sz } return io.EOF }