diff options
-rw-r--r-- | pkg/sentry/usermem/usermem.go | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/pkg/sentry/usermem/usermem.go b/pkg/sentry/usermem/usermem.go index 1d6c0b4d6..75ac4d22d 100644 --- a/pkg/sentry/usermem/usermem.go +++ b/pkg/sentry/usermem/usermem.go @@ -181,7 +181,11 @@ func CopyObjectOut(ctx context.Context, uio IO, addr Addr, src interface{}, opts Addr: addr, Opts: opts, } - return w.Write(binary.Marshal(nil, ByteOrder, src)) + // Allocate a byte slice the size of the object being marshaled. This + // adds an extra reflection call, but avoids needing to grow the slice + // during encoding, which can result in many heap-allocated slices. + b := make([]byte, 0, binary.Size(src)) + return w.Write(binary.Marshal(b, ByteOrder, src)) } // CopyObjectIn copies a fixed-size value or slice of fixed-size values from |