aboutsummaryrefslogtreecommitdiff
path: root/src/lib/io/bytebuffer.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2008-12-16 13:01:39 -0800
committerRob Pike <r@golang.org>2008-12-16 13:01:39 -0800
commita10267adcdd40093cb2c9d8a470194332b947b86 (patch)
tree6c3ea39f93c67c0a292b294c728066771b55cd64 /src/lib/io/bytebuffer.go
parent30a1a8c92251941dd850d66ec434231cc1140fb3 (diff)
downloadgo-a10267adcdd40093cb2c9d8a470194332b947b86.tar.xz
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
Diffstat (limited to 'src/lib/io/bytebuffer.go')
-rw-r--r--src/lib/io/bytebuffer.go8
1 files changed, 8 insertions, 0 deletions
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]
}