copy the slice passed to stream.Write

This commit is contained in:
Lucas Clemente
2016-07-08 13:57:08 +02:00
parent f8130288a3
commit bb8464efe0
2 changed files with 14 additions and 1 deletions

View File

@@ -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()

View File

@@ -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() {