From: Rob Pike Date: Tue, 16 Dec 2008 21:01:39 +0000 (-0800) Subject: If ByteBuffer has never been used, b.buf is nil but Data() should still work. X-Git-Tag: weekly.2009-11-06~2501 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a10267adcdd40093cb2c9d8a470194332b947b86;p=gostls13.git If ByteBuffer has never been used, b.buf is nil but Data() should still work. Fix the bug using a (safe) shared global empty array. R=rsc DELTA=8 (8 added, 0 deleted, 0 changed) OCL=21303 CL=21303 --- diff --git a/src/lib/io/bytebuffer.go b/src/lib/io/bytebuffer.go index 9035367171..8af8a09aa1 100644 --- a/src/lib/io/bytebuffer.go +++ b/src/lib/io/bytebuffer.go @@ -75,7 +75,15 @@ func (b *ByteBuffer) Len() int { return b.len } +// If the buffer is empty, Data() should still give a valid array. +// Use this variable as a surrogate. It's immutable (can't be +// grown, can't store any data) so it's safe to share. +var EmptyByteArray = new([]byte, 0) + func (b *ByteBuffer) Data() *[]byte { + if b.buf == nil { + return EmptyByteArray + } return b.buf[b.off:b.len] }