From 20944cf0665a5aec6abab802875a9700592b5391 Mon Sep 17 00:00:00 2001 From: Alexander Yastrebov Date: Thu, 28 Sep 2023 22:26:57 +0000 Subject: [PATCH] internal/zstd: reset reader buffer Reset r.buffer on Reset to avoid subsequent Read calls observing previously decoded data. For #62513 Change-Id: Icb65e76b5c5c0af32b36ec3a5999dca86407cbc8 GitHub-Last-Rev: 99c0a6fa72ad67cba5d29593fd3b28d14ddce4a4 GitHub-Pull-Request: golang/go#63288 Reviewed-on: https://go-review.googlesource.com/c/go/+/531735 Auto-Submit: Ian Lance Taylor Reviewed-by: Ian Lance Taylor Reviewed-by: Than McIntosh LUCI-TryBot-Result: Go LUCI --- src/internal/zstd/zstd.go | 2 +- src/internal/zstd/zstd_test.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/internal/zstd/zstd.go b/src/internal/zstd/zstd.go index 72d733f6dd..9cf62a6bac 100644 --- a/src/internal/zstd/zstd.go +++ b/src/internal/zstd/zstd.go @@ -104,7 +104,7 @@ func (r *Reader) Reset(input io.Reader) { r.frameSizeUnknown = false r.remainingFrameSize = 0 r.blockOffset = 0 - // buffer + r.buffer = r.buffer[:0] r.off = 0 // repeatedOffset1 // repeatedOffset2 diff --git a/src/internal/zstd/zstd_test.go b/src/internal/zstd/zstd_test.go index 8c7f9403c1..70141b02d0 100644 --- a/src/internal/zstd/zstd_test.go +++ b/src/internal/zstd/zstd_test.go @@ -127,6 +127,26 @@ func TestSamples(t *testing.T) { } } +func TestReset(t *testing.T) { + input := strings.NewReader("") + r := NewReader(input) + for _, test := range tests { + test := test + t.Run(test.name, func(t *testing.T) { + input.Reset(test.compressed) + r.Reset(input) + got, err := io.ReadAll(r) + if err != nil { + t.Fatal(err) + } + gotstr := string(got) + if gotstr != test.uncompressed { + t.Errorf("got %q want %q", gotstr, test.uncompressed) + } + }) + } +} + var ( bigDataOnce sync.Once bigDataBytes []byte