From 61996d1046199ce67109353358866cf97a80a361 Mon Sep 17 00:00:00 2001 From: lizhao Date: Mon, 24 Jun 2024 22:56:48 +0800 Subject: [PATCH] fix use of resize --- services/applypatch/block_set.cpp | 3 --- services/applypatch/command_process.cpp | 18 +++++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/services/applypatch/block_set.cpp b/services/applypatch/block_set.cpp index c21aa0cc..6d60c096 100644 --- a/services/applypatch/block_set.cpp +++ b/services/applypatch/block_set.cpp @@ -251,9 +251,6 @@ void BlockSet::MoveBlock(std::vector &target, const BlockSet& locations int32_t BlockSet::LoadSourceBuffer(const Command &cmd, size_t &pos, std::vector &sourceBuffer, bool &isOverlap, size_t &srcBlockSize) { - std::string blockLen = cmd.GetArgumentByPos(pos++); - srcBlockSize = String2Int(blockLen, N_DEC); - sourceBuffer.resize(srcBlockSize * H_BLOCK_SIZE); std::string targetCmd = cmd.GetArgumentByPos(pos++); std::string storeBase = cmd.GetTransferParams()->storeBase; if (targetCmd != "-") { diff --git a/services/applypatch/command_process.cpp b/services/applypatch/command_process.cpp index 2e6af0e7..f1c71f71 100644 --- a/services/applypatch/command_process.cpp +++ b/services/applypatch/command_process.cpp @@ -33,6 +33,7 @@ #include "utils.h" using namespace Hpackage; +using namespace Updater::Utils; namespace Updater { CommandResult AbortCommandFn::Execute(const Command ¶ms) { @@ -111,23 +112,26 @@ bool LoadTarget(const Command ¶ms, size_t &pos, std::vector &buffer } // Read the target's buffer to determine whether it needs to be written - size_t tgtBlockSize; std::string cmdTmp = params.GetArgumentByPos(pos++); targetBlock.ParserAndInsert(cmdTmp); - tgtBlockSize = targetBlock.TotalBlockSize() * H_BLOCK_SIZE; - buffer.resize(tgtBlockSize); - if (targetBlock.ReadDataFromBlock(params.GetFileDescriptor(), buffer) == 0) { + size_t tgtBlockSize = targetBlock.TotalBlockSize() * H_BLOCK_SIZE; + std::vector tgtBuffer(tgtBlockSize); + + if (targetBlock.ReadDataFromBlock(params.GetFileDescriptor(), tgtBuffer) == 0) { LOG(ERROR) << "Read data from block error, TotalBlockSize: " << targetBlock.TotalBlockSize(); result = FAILED; return false; } - if (targetBlock.VerifySha256(buffer, targetBlock.TotalBlockSize(), tgtHash) == 0) { + if (targetBlock.VerifySha256(tgtBuffer, targetBlock.TotalBlockSize(), tgtHash) == 0) { LOG(ERROR) << "Will write same sha256 blocks to target, no need to write"; result = SUCCESS; return false; } - - if (targetBlock.LoadTargetBuffer(params, buffer, tgtBlockSize, pos, srcHash) != 0) { + std::vector().swap(tgtBuffer); + std::string blockLen = params.GetArgumentByPos(pos++); + size_t srcBlockSize = String2Int(blockLen, N_DEC); + buffer.resize(srcBlockSize * H_BLOCK_SIZE); + if (targetBlock.LoadTargetBuffer(params, buffer, srcBlockSize, pos, srcHash) != 0) { LOG(ERROR) << "Failed to load blocks"; result = FAILED; return false; -- Gitee