Skip to content

Commit

Permalink
Fix a bug in SetBlock() where varied block size will kill the download
Browse files Browse the repository at this point in the history
Issue: #13393
  • Loading branch information
selissia committed Jan 25, 2022
1 parent 9e5f299 commit ea25e12
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/platform/Linux/OTAImageProcessorImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,29 +180,30 @@ void OTAImageProcessorImpl::HandleProcessBlock(intptr_t context)

CHIP_ERROR OTAImageProcessorImpl::SetBlock(ByteSpan & block)
{
if ((block.data() == nullptr) || block.empty())
if (!IsSpanUsable(block))
{
ReleaseBlock();
return CHIP_NO_ERROR;
}

// Allocate memory for block data if it has not been done yet
if (mBlock.empty())
if (mBlock.size() < block.size())
{
mBlock = MutableByteSpan(static_cast<uint8_t *>(chip::Platform::MemoryAlloc(block.size())), block.size());
if (mBlock.data() == nullptr)
if (!mBlock.empty())
{
ReleaseBlock();
}
uint8_t * mBlock_ptr = static_cast<uint8_t *>(chip::Platform::MemoryAlloc(block.size()));
if (mBlock_ptr == nullptr)
{
return CHIP_ERROR_NO_MEMORY;
}
mBlock = MutableByteSpan(mBlock_ptr, block.size());
}

// Store the actual block data
CHIP_ERROR err = CopySpanToMutableSpan(block, mBlock);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Cannot copy block data: %" CHIP_ERROR_FORMAT, err.Format());
return err;
}

return CHIP_NO_ERROR;
}

Expand Down

0 comments on commit ea25e12

Please sign in to comment.