From 241502c2a231556bb76f681e5e55cb71d3234c6b Mon Sep 17 00:00:00 2001 From: Paul Hollinsky Date: Thu, 24 Feb 2022 23:41:50 -0500 Subject: [PATCH] Disk: Fix improper offset calculation This would cause an underflow previously --- disk/diskreaddriver.cpp | 7 ++++++- disk/diskwritedriver.cpp | 10 +++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/disk/diskreaddriver.cpp b/disk/diskreaddriver.cpp index 4a253e6..b795dc6 100644 --- a/disk/diskreaddriver.cpp +++ b/disk/diskreaddriver.cpp @@ -24,7 +24,12 @@ optional ReadDriver::readLogicalDisk(Communication& com, device_eventh while(blocksProcessed < blocks && timeout >= std::chrono::milliseconds::zero()) { const uint64_t currentBlock = startBlock + blocksProcessed; - const uint64_t intoOffset = std::max((blocksProcessed * idealBlockSize) - posWithinFirstBlock, 0); + uint64_t intoOffset = blocksProcessed * idealBlockSize;; + if(intoOffset < posWithinFirstBlock) + intoOffset = 0; + else + intoOffset -= posWithinFirstBlock; + const uint32_t posWithinCurrentBlock = (blocksProcessed ? 0 : posWithinFirstBlock); uint32_t curAmt = idealBlockSize - posWithinCurrentBlock; const auto amountLeft = amount - ret.value_or(0); diff --git a/disk/diskwritedriver.cpp b/disk/diskwritedriver.cpp index 1eac0d3..f5434ef 100644 --- a/disk/diskwritedriver.cpp +++ b/disk/diskwritedriver.cpp @@ -32,7 +32,12 @@ optional WriteDriver::writeLogicalDisk(Communication& com, device_even while(blocksProcessed < blocks && timeout >= std::chrono::milliseconds::zero()) { const uint64_t currentBlock = startBlock + blocksProcessed; - const uint64_t fromOffset = std::max((blocksProcessed * idealBlockSize) - posWithinFirstBlock, 0); + uint64_t fromOffset = blocksProcessed * idealBlockSize; + if(fromOffset < posWithinFirstBlock) + fromOffset = 0; + else + fromOffset -= posWithinFirstBlock; + const uint32_t posWithinCurrentBlock = (blocksProcessed ? 0 : posWithinFirstBlock); uint32_t curAmt = idealBlockSize - posWithinCurrentBlock; const auto amountLeft = amount - ret.value_or(0); @@ -54,8 +59,7 @@ optional WriteDriver::writeLogicalDisk(Communication& com, device_even const bool useAlignedWriteBuffer = (posWithinCurrentBlock != 0 || curAmt != idealBlockSize); if(useAlignedWriteBuffer) { - if(alignedWriteBuffer.size() < idealBlockSize) - alignedWriteBuffer.resize(idealBlockSize); + alignedWriteBuffer = atomicBuffer; memcpy(alignedWriteBuffer.data() + posWithinCurrentBlock, from + fromOffset, curAmt); }