From bb8464efe096e1f8149ce73c26ea96784683e063 Mon Sep 17 00:00:00 2001 From: Lucas Clemente Date: Fri, 8 Jul 2016 13:57:08 +0200 Subject: [PATCH] copy the slice passed to stream.Write --- stream.go | 3 ++- stream_test.go | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/stream.go b/stream.go index 746a83e32..39743b580 100644 --- a/stream.go +++ b/stream.go @@ -160,7 +160,8 @@ func (s *stream) Write(p []byte) (int, error) { s.mutex.Lock() defer s.mutex.Unlock() - s.dataForWriting = p + s.dataForWriting = make([]byte, len(p)) + copy(s.dataForWriting, p) s.session.scheduleSending() diff --git a/stream_test.go b/stream_test.go index b02ba2e50..bd47226d2 100644 --- a/stream_test.go +++ b/stream_test.go @@ -346,6 +346,18 @@ var _ = Describe("Stream", func() { It("getDataForWriting returns nil if no data is available", func() { Expect(str.getDataForWriting(1000)).To(BeNil()) }) + + It("copies the slice while writing", func() { + s := []byte("foo") + go func() { + n, err := str.Write(s) + Expect(err).ToNot(HaveOccurred()) + Expect(n).To(Equal(3)) + }() + Eventually(func() protocol.ByteCount { return str.lenOfDataForWriting() }).ShouldNot(BeZero()) + s[0] = 'v' + Expect(str.getDataForWriting(3)).To(Equal([]byte("foo"))) + }) }) Context("closing", func() {