2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-11-14 11:31:28 +08:00

Merge pull request #17 from tamird/fix-write

buffer: do not retain passed-in memory
This commit is contained in:
Soheil Hassas Yeganeh 2016-02-25 12:03:57 -05:00
commit 23074f6929
2 changed files with 40 additions and 10 deletions

View File

@ -2,6 +2,8 @@ package cmux
import "io" import "io"
var _ io.ReadWriter = (*buffer)(nil)
type buffer struct { type buffer struct {
read int read int
data []byte data []byte
@ -38,13 +40,18 @@ func (b *buffer) resetRead() {
b.read = 0 b.read = 0
} }
func (b *buffer) Write(p []byte) (n int, err error) { // From the io.Writer documentation:
n = len(p) //
if b.data == nil { // Write writes len(p) bytes from p to the underlying data stream.
b.data = p[:n:n] // It returns the number of bytes written from p (0 <= n <= len(p))
return // and any error encountered that caused the write to stop early.
} // Write must return a non-nil error if it returns n < len(p).
// Write must not modify the slice data, even temporarily.
//
// Implementations must not retain p.
//
// In a previous incarnation, this implementation retained the incoming slice.
func (b *buffer) Write(p []byte) (int, error) {
b.data = append(b.data, p...) b.data = append(b.data, p...)
return return len(p), nil
} }

View File

@ -6,8 +6,31 @@ import (
"testing" "testing"
) )
func TestWriteNoModify(t *testing.T) {
var b buffer
const origWriteByte = 0
const postWriteByte = 1
writeBytes := []byte{origWriteByte}
if _, err := b.Write(writeBytes); err != nil {
t.Fatal(err)
}
writeBytes[0] = postWriteByte
readBytes := make([]byte, 1)
if _, err := b.Read(readBytes); err != io.EOF {
t.Fatal(err)
}
if readBytes[0] != origWriteByte {
t.Fatalf("expected to read %x, but read %x; buffer retained passed-in slice", origWriteByte, postWriteByte)
}
}
const writeString = "deadbeef"
func TestBuffer(t *testing.T) { func TestBuffer(t *testing.T) {
writeBytes := []byte("deadbeef") writeBytes := []byte(writeString)
const numWrites = 10 const numWrites = 10
@ -54,7 +77,7 @@ func TestBuffer(t *testing.T) {
} }
func TestBufferOffset(t *testing.T) { func TestBufferOffset(t *testing.T) {
writeBytes := []byte("deadbeef") writeBytes := []byte(writeString)
var b buffer var b buffer
n, err := b.Write(writeBytes) n, err := b.Write(writeBytes)