From fbbc3e6d725a68abf8f5bf11572a0a4ef49b54e5 Mon Sep 17 00:00:00 2001 From: doru91 Date: Mon, 24 Jan 2022 19:54:58 +0200 Subject: [PATCH] [K32W0] Fix OTA memory allocation (#13530) * [K32W0] Fix OTA memory allocation Use the non-buggy version from ESP32. See also: https://github.com/project-chip/connectedhomeip/issues/1339 Signed-off-by: Doru Gucea * Restyled by clang-format * [K32W0] Remove unneeded memory free Signed-off-by: Doru Gucea Co-authored-by: Restyled.io --- .../nxp/k32w/k32w0/OTAImageProcessorImpl.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/platform/nxp/k32w/k32w0/OTAImageProcessorImpl.cpp b/src/platform/nxp/k32w/k32w0/OTAImageProcessorImpl.cpp index f377ceba34906e..a0f9065c7aa19e 100644 --- a/src/platform/nxp/k32w/k32w0/OTAImageProcessorImpl.cpp +++ b/src/platform/nxp/k32w/k32w0/OTAImageProcessorImpl.cpp @@ -142,29 +142,29 @@ void OTAImageProcessorImpl::HandleProcessBlock(intptr_t context) CHIP_ERROR OTAImageProcessorImpl::SetBlock(ByteSpan & block) { - if ((block.data() == nullptr) || block.empty()) + if (!IsSpanUsable(block)) { 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(chip::Platform::MemoryAlloc(block.size())), block.size()); - if (mBlock.data() == nullptr) + if (!mBlock.empty()) + { + ReleaseBlock(); + } + uint8_t * mBlock_ptr = static_cast(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; }