]> Cypherpunks repositories - gostls13.git/commitdiff
If ByteBuffer has never been used, b.buf is nil but Data() should still work.
authorRob Pike <r@golang.org>
Tue, 16 Dec 2008 21:01:39 +0000 (13:01 -0800)
committerRob Pike <r@golang.org>
Tue, 16 Dec 2008 21:01:39 +0000 (13:01 -0800)
Fix the bug using a (safe) shared global empty array.

R=rsc
DELTA=8  (8 added, 0 deleted, 0 changed)
OCL=21303
CL=21303

src/lib/io/bytebuffer.go

index 903536717188f7e19db7b9e7cf25852a1361e8a9..8af8a09aa1056295d7ea073ac418524d2c4ebd2b 100644 (file)
@@ -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]
 }