From 45c29be44205f65646246333f3ae1a4701c21151 Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Mon, 8 Dec 2014 21:08:07 -0800 Subject: [PATCH] Address bug in inmemory filesystem WriteAt method --- storagedriver/inmemory/mfs.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/storagedriver/inmemory/mfs.go b/storagedriver/inmemory/mfs.go index 5248bbc6..9d75cb0e 100644 --- a/storagedriver/inmemory/mfs.go +++ b/storagedriver/inmemory/mfs.go @@ -294,15 +294,16 @@ func (f *file) ReadAt(p []byte, offset int64) (n int, err error) { } func (f *file) WriteAt(p []byte, offset int64) (n int, err error) { - if len(f.data) > 0 && offset >= int64(len(f.data)) { - // Extend missing region with a zero pad, while also preallocating out to size of p. - pad := offset - int64(len(f.data)) - size := len(p) + int(pad) - f.data = append(f.data, make([]byte, pad, size)...) + off := int(offset) + if cap(f.data) < off+len(p) { + data := make([]byte, len(f.data), off+len(p)) + copy(data, f.data) + f.data = data } - f.data = append(f.data, p...) - return len(p), nil + f.data = f.data[:off+len(p)] + + return copy(f.data[off:off+len(p)], p), nil } func (f *file) String() string {