From 5a52fcc5466a7da26994ac168d56ee5e9cd21ae9 Mon Sep 17 00:00:00 2001 From: Bhasker Hariharan Date: Wed, 9 Feb 2022 22:58:41 -0800 Subject: [PATCH] Fix buffer aliasing issue when Merging views. PiperOrigin-RevId: 427662565 --- pkg/buffer/view.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/buffer/view.go b/pkg/buffer/view.go index 53167c28a..13839e255 100644 --- a/pkg/buffer/view.go +++ b/pkg/buffer/view.go @@ -385,6 +385,10 @@ func (v *View) Clone() View { size: v.size, } for buf := v.data.Front(); buf != nil; buf = buf.Next() { + // Copy the buffer structs itself as they are stateful and + // should not be shared between Views. + // + // TODO(gvisor.dev/issue/7158): revisit need for View.pool. newBuf := other.pool.getNoInit() *newBuf = *buf other.data.PushBack(newBuf) @@ -428,7 +432,13 @@ func (v *View) Merge(other *View) { // Copy over all buffers. for buf := other.data.Front(); buf != nil; buf = other.data.Front() { other.data.Remove(buf) - v.data.PushBack(buf) + // Copy the buffer structs itself as they are stateful and + // should not be shared between Views. + // + // TODO(gvisor.dev/issue/7158): revisit need for View.pool. + newBuf := v.pool.getNoInit() + *newBuf = *buf + v.data.PushBack(newBuf) } // Adjust sizes.