From b4253a8986294de4272be8b8f82ec9d475fb07a0 Mon Sep 17 00:00:00 2001 From: Badrish Chandramouli Date: Mon, 7 Mar 2022 11:15:47 -0800 Subject: [PATCH] Fix length of source data being copied to file --- cs/src/core/Allocator/GenericAllocator.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cs/src/core/Allocator/GenericAllocator.cs b/cs/src/core/Allocator/GenericAllocator.cs index 077a18f3d..e55aa6e4f 100644 --- a/cs/src/core/Allocator/GenericAllocator.cs +++ b/cs/src/core/Allocator/GenericAllocator.cs @@ -441,13 +441,14 @@ private void WriteAsync(long flushPage, ulong alignedDestinationAddres if (endPosition > ObjectBlockSize || i == (end / recordSize) - 1) { - var memoryStreamLength = (int)endPosition; + var memoryStreamActualLength = ms.Position; + var memoryStreamTotalLength = (int)endPosition; - var _objBuffer = bufferPool.Get(memoryStreamLength); + var _objBuffer = bufferPool.Get(memoryStreamTotalLength); asyncResult.done = new AutoResetEvent(false); - var _alignedLength = (memoryStreamLength + (sectorSize - 1)) & ~(sectorSize - 1); + var _alignedLength = (memoryStreamTotalLength + (sectorSize - 1)) & ~(sectorSize - 1); var _objAddr = Interlocked.Add(ref localSegmentOffsets[(long)(alignedDestinationAddress >> LogSegmentSizeBits) % SegmentBufferSize], _alignedLength) - _alignedLength; @@ -459,7 +460,7 @@ private void WriteAsync(long flushPage, ulong alignedDestinationAddres ms.Close(); fixed (void* src_ = ms.GetBuffer()) - Buffer.MemoryCopy(src_, _objBuffer.aligned_pointer, memoryStreamLength, memoryStreamLength); + Buffer.MemoryCopy(src_, _objBuffer.aligned_pointer, memoryStreamTotalLength, memoryStreamActualLength); foreach (var address in addr) ((AddressInfo*)address)->Address += _objAddr;