From 9d2f07f47842b3e86e772a90650c6e862f47b242 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Tue, 9 Oct 2018 19:36:59 -0700 Subject: [PATCH] copy tcp writing into byte array --- src/comm/comm.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/comm/comm.go b/src/comm/comm.go index 02decfa7..c2ef6376 100644 --- a/src/comm/comm.go +++ b/src/comm/comm.go @@ -33,8 +33,12 @@ func (c Comm) Close() { } func (c Comm) Write(b []byte) (int, error) { - n, err := c.connection.Write(append([]byte(fmt.Sprintf("%0.5d", len(b))), b...)) - if n != len(b)+5 { + tmpCopy := make([]byte, len(b)+5) + // Copy the buffer so it doesn't get changed while read by the recipient. + copy(tmpCopy[:5], []byte(fmt.Sprintf("%0.5d", len(b)))) + copy(tmpCopy[5:], b) + n, err := c.connection.Write(tmpCopy) + if n != len(tmpCopy) { err = fmt.Errorf("wanted to write %d but wrote %d", len(b), n) } // log.Printf("wanted to write %d but wrote %d", n, len(b))