mirror of
https://github.com/intrepidcs/libicsneo.git
synced 2026-08-05 09:28:40 +02:00
Disk: Fix improper offset calculation
This would cause an underflow previously
This commit is contained in:
@@ -24,7 +24,12 @@ optional<uint64_t> ReadDriver::readLogicalDisk(Communication& com, device_eventh
|
|||||||
while(blocksProcessed < blocks && timeout >= std::chrono::milliseconds::zero()) {
|
while(blocksProcessed < blocks && timeout >= std::chrono::milliseconds::zero()) {
|
||||||
const uint64_t currentBlock = startBlock + blocksProcessed;
|
const uint64_t currentBlock = startBlock + blocksProcessed;
|
||||||
|
|
||||||
const uint64_t intoOffset = std::max<uint64_t>((blocksProcessed * idealBlockSize) - posWithinFirstBlock, 0);
|
uint64_t intoOffset = blocksProcessed * idealBlockSize;;
|
||||||
|
if(intoOffset < posWithinFirstBlock)
|
||||||
|
intoOffset = 0;
|
||||||
|
else
|
||||||
|
intoOffset -= posWithinFirstBlock;
|
||||||
|
|
||||||
const uint32_t posWithinCurrentBlock = (blocksProcessed ? 0 : posWithinFirstBlock);
|
const uint32_t posWithinCurrentBlock = (blocksProcessed ? 0 : posWithinFirstBlock);
|
||||||
uint32_t curAmt = idealBlockSize - posWithinCurrentBlock;
|
uint32_t curAmt = idealBlockSize - posWithinCurrentBlock;
|
||||||
const auto amountLeft = amount - ret.value_or(0);
|
const auto amountLeft = amount - ret.value_or(0);
|
||||||
|
|||||||
@@ -32,7 +32,12 @@ optional<uint64_t> WriteDriver::writeLogicalDisk(Communication& com, device_even
|
|||||||
while(blocksProcessed < blocks && timeout >= std::chrono::milliseconds::zero()) {
|
while(blocksProcessed < blocks && timeout >= std::chrono::milliseconds::zero()) {
|
||||||
const uint64_t currentBlock = startBlock + blocksProcessed;
|
const uint64_t currentBlock = startBlock + blocksProcessed;
|
||||||
|
|
||||||
const uint64_t fromOffset = std::max<uint64_t>((blocksProcessed * idealBlockSize) - posWithinFirstBlock, 0);
|
uint64_t fromOffset = blocksProcessed * idealBlockSize;
|
||||||
|
if(fromOffset < posWithinFirstBlock)
|
||||||
|
fromOffset = 0;
|
||||||
|
else
|
||||||
|
fromOffset -= posWithinFirstBlock;
|
||||||
|
|
||||||
const uint32_t posWithinCurrentBlock = (blocksProcessed ? 0 : posWithinFirstBlock);
|
const uint32_t posWithinCurrentBlock = (blocksProcessed ? 0 : posWithinFirstBlock);
|
||||||
uint32_t curAmt = idealBlockSize - posWithinCurrentBlock;
|
uint32_t curAmt = idealBlockSize - posWithinCurrentBlock;
|
||||||
const auto amountLeft = amount - ret.value_or(0);
|
const auto amountLeft = amount - ret.value_or(0);
|
||||||
@@ -54,8 +59,7 @@ optional<uint64_t> WriteDriver::writeLogicalDisk(Communication& com, device_even
|
|||||||
|
|
||||||
const bool useAlignedWriteBuffer = (posWithinCurrentBlock != 0 || curAmt != idealBlockSize);
|
const bool useAlignedWriteBuffer = (posWithinCurrentBlock != 0 || curAmt != idealBlockSize);
|
||||||
if(useAlignedWriteBuffer) {
|
if(useAlignedWriteBuffer) {
|
||||||
if(alignedWriteBuffer.size() < idealBlockSize)
|
alignedWriteBuffer = atomicBuffer;
|
||||||
alignedWriteBuffer.resize(idealBlockSize);
|
|
||||||
memcpy(alignedWriteBuffer.data() + posWithinCurrentBlock, from + fromOffset, curAmt);
|
memcpy(alignedWriteBuffer.data() + posWithinCurrentBlock, from + fromOffset, curAmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user