forked from quic-go/quic-go
correctly treat nils and empty slices in stream.Write
This commit is contained in:
11
stream.go
11
stream.go
@@ -127,6 +127,15 @@ func (s *stream) ReadByte() (byte, error) {
|
||||
|
||||
func (s *stream) Write(p []byte) (int, error) {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
if s.err != nil {
|
||||
return 0, s.err
|
||||
}
|
||||
|
||||
if len(p) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
s.dataForWriting = make([]byte, len(p))
|
||||
copy(s.dataForWriting, p)
|
||||
@@ -137,10 +146,10 @@ func (s *stream) Write(p []byte) (int, error) {
|
||||
s.doneWritingOrErrCond.Wait()
|
||||
}
|
||||
|
||||
defer s.mutex.Unlock()
|
||||
if s.err != nil {
|
||||
return 0, s.err
|
||||
}
|
||||
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -371,6 +371,18 @@ var _ = Describe("Stream", func() {
|
||||
s[0] = 'v'
|
||||
Expect(str.getDataForWriting(3)).To(Equal([]byte("foo")))
|
||||
})
|
||||
|
||||
It("returns when given a nil input", func() {
|
||||
n, err := str.Write(nil)
|
||||
Expect(n).To(BeZero())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("returns when given an empty slice", func() {
|
||||
n, err := str.Write([]byte(""))
|
||||
Expect(n).To(BeZero())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
Context("closing", func() {
|
||||
|
||||
Reference in New Issue
Block a user