From 3af4982e8679e0011d90ae2537bb2fbcfbdc4a07 Mon Sep 17 00:00:00 2001 From: ma_nan Date: Fri, 24 Sep 2021 15:33:41 +0800 Subject: [PATCH 1/4] fix codestyle Signed-off-by: ma_nan --- services/applypatch/partition_record.cpp | 12 +++++---- services/fs_manager/mount.cpp | 2 +- .../script_interpreter/script_expression.cpp | 12 +-------- .../script_interpreter/script_expression.h | 1 - .../script_interpreter/script_interpreter.cpp | 12 +++++++++ .../script_interpreter/script_interpreter.h | 6 +++-- services/ui/drm_driver.cpp | 2 +- services/ui/drm_driver.h | 4 +-- services/ui/text_label.cpp | 26 ++++++++++++------- services/ui/text_label.h | 4 +++ services/updater.cpp | 9 ++++--- services/updater_binary/update_partitions.cpp | 11 ++++---- utils/utils.cpp | 9 ++++--- 13 files changed, 64 insertions(+), 46 deletions(-) diff --git a/services/applypatch/partition_record.cpp b/services/applypatch/partition_record.cpp index 9d758176..e6de89be 100644 --- a/services/applypatch/partition_record.cpp +++ b/services/applypatch/partition_record.cpp @@ -31,9 +31,9 @@ bool PartitionRecord::IsPartitionUpdated(const std::string &partitionName) { auto miscBlockDevice = GetMiscPartitionPath(); uint8_t buffer[PARTITION_UPDATER_RECORD_MSG_SIZE]; - char *realPath = realpath(miscBlockDevice.c_str(), NULL); - UPDATER_FILE_CHECK(realPath != nullptr, "realPath is NULL", return false); if (!miscBlockDevice.empty()) { + char *realPath = realpath(miscBlockDevice.c_str(), NULL); + UPDATER_FILE_CHECK(realPath != nullptr, "realPath is NULL", return false); int fd = open(realPath, O_RDONLY | O_EXCL | O_CLOEXEC | O_BINARY); free(realPath); UPDATER_FILE_CHECK(fd >= 0, "PartitionRecord: Open misc to recording partition failed", return false); @@ -60,9 +60,9 @@ bool PartitionRecord::IsPartitionUpdated(const std::string &partitionName) bool PartitionRecord::RecordPartitionUpdateStatus(const std::string &partitionName, bool updated) { auto miscBlockDevice = GetMiscPartitionPath(); - char *realPath = realpath(miscBlockDevice.c_str(), NULL); - UPDATER_FILE_CHECK(realPath != nullptr, "realPath is NULL", return false); if (!miscBlockDevice.empty()) { + char *realPath = realpath(miscBlockDevice.c_str(), NULL); + UPDATER_FILE_CHECK(realPath != nullptr, "realPath is NULL", return false); int fd = open(realPath, O_RDWR | O_EXCL | O_CLOEXEC | O_BINARY); free(realPath); UPDATER_FILE_CHECK(fd >= 0, "PartitionRecord: Open misc to recording partition failed", return false); @@ -76,6 +76,8 @@ bool PartitionRecord::RecordPartitionUpdateStatus(const std::string &partitionNa UPDATER_CHECK_FILE_OP(lseek(fd, PARTITION_RECORD_START + offset_, SEEK_SET) >= 0, "PartitionRecord: Seek misc to specific offset failed", fd, return false); if (offset_ + sizeof(PartitionRecordInfo) < PARTITION_UPDATER_RECORD_SIZE) { + UPDATER_CHECK_FILE_OP(memset_s(&info_, sizeof(info_), 0, sizeof(info_)) == 0, + "PartitionRecord: clear partition info failed", fd, return false); UPDATER_CHECK_FILE_OP(!strncpy_s(info_.partitionName, PARTITION_NAME_LEN, partitionName.c_str(), PARTITION_NAME_LEN - 1), "PartitionRecord: strncpy_s failed", fd, return false); info_.updated = updated; @@ -85,7 +87,7 @@ bool PartitionRecord::RecordPartitionUpdateStatus(const std::string &partitionNa UPDATER_CHECK_FILE_OP(lseek(fd, PARTITION_RECORD_OFFSET, SEEK_SET) >= 0, "PartitionRecord: Seek misc to record offset failed", fd, return false); UPDATER_CHECK_FILE_OP(write(fd, &offset_, sizeof(off_t)) == sizeof(off_t), - "PartitionRecord: Seek misc to record offset failed", fd, return false); + "PartitionRecord: write misc to record offset failed", fd, return false); LOG(DEBUG) << "PartitionRecord: offset is " << offset_; } else { LOG(WARNING) << "PartitionRecord: partition record overflow, offset = " << offset_; diff --git a/services/fs_manager/mount.cpp b/services/fs_manager/mount.cpp index 1053f2fa..20f99e76 100644 --- a/services/fs_manager/mount.cpp +++ b/services/fs_manager/mount.cpp @@ -143,7 +143,7 @@ static MountStatus GetMountStatusForMountPoint(const std::string &mountPoint) while (fgets(buffer, sizeof(buffer) - 1, fp.get()) != nullptr) { n = strlen(buffer); - if (buffer[n - 1] == '\n') { + if (n > 0 && buffer[n - 1] == '\n') { buffer[n - 1] = '\0'; } std::string line(buffer); diff --git a/services/script/script_interpreter/script_expression.cpp b/services/script/script_interpreter/script_expression.cpp index d3f6293b..048d1b38 100644 --- a/services/script/script_interpreter/script_expression.cpp +++ b/services/script/script_interpreter/script_expression.cpp @@ -154,16 +154,7 @@ UScriptValuePtr FunctionCallExpression::Execute(ScriptInterpreter &inter, UScrip if (inter.IsNativeFunction(functionName_)) { return inter.ExecuteNativeFunc(local, functionName_, params_); } - - ScriptFunction* function = function_; - if (function_ == nullptr) { - function = inter.FindFunction(functionName_); - } - if (function == nullptr) { - INTERPRETER_LOGI(inter, local, "Can not find function %s", functionName_.c_str()); - return std::make_shared(USCRIPT_NOTEXIST_INSTRUCTION); - } - return function->Execute(inter, local, params_); + return inter.ExecuteFunction(local, functionName_, params_); } BinaryExpression::~BinaryExpression() @@ -177,7 +168,6 @@ AssignExpression::~AssignExpression() } FunctionCallExpression::~FunctionCallExpression() { - delete function_; delete params_; } } // namespace uscript diff --git a/services/script/script_interpreter/script_expression.h b/services/script/script_interpreter/script_expression.h index c864969c..1d4e163f 100644 --- a/services/script/script_interpreter/script_expression.h +++ b/services/script/script_interpreter/script_expression.h @@ -200,7 +200,6 @@ public: static UScriptExpression* CreateExpression(std::string identifier, ScriptParams *params); private: std::string functionName_; - ScriptFunction* function_ = nullptr; ScriptParams* params_ = nullptr; }; } // namespace uscript diff --git a/services/script/script_interpreter/script_interpreter.cpp b/services/script/script_interpreter/script_interpreter.cpp index bef46fa8..0531a914 100644 --- a/services/script/script_interpreter/script_interpreter.cpp +++ b/services/script/script_interpreter/script_interpreter.cpp @@ -109,6 +109,18 @@ ScriptFunction* ScriptInterpreter::FindFunction(const std::string &name) return nullptr; } +UScriptValuePtr ScriptInterpreter::ExecuteFunction(UScriptContextPtr context, const std::string &name, + ScriptParams *params) +{ + ScriptFunction *function = FindFunction(name); + if (function == nullptr) { + USCRIPT_LOGI("Fail to find function %s", name.c_str()); + return std::make_shared(USCRIPT_NOTEXIST_INSTRUCTION); + } else { + return function->Execute(*this, context, params); + } +} + UScriptValuePtr ScriptInterpreter::FindVariable(UScriptContextPtr local, std::string id) { for (auto context = contextStack_.rbegin(); context != contextStack_.rend(); context++) { diff --git a/services/script/script_interpreter/script_interpreter.h b/services/script/script_interpreter/script_interpreter.h index 7a30b0a1..adced81b 100644 --- a/services/script/script_interpreter/script_interpreter.h +++ b/services/script/script_interpreter/script_interpreter.h @@ -49,10 +49,11 @@ public: void AddStatement(UScriptStatement *statement); int32_t AddFunction(ScriptFunction *function); - ScriptFunction* FindFunction(const std::string &name); bool IsNativeFunction(std::string name); UScriptValuePtr ExecuteNativeFunc(UScriptContextPtr upContext, const std::string &name, ScriptParams *params); + UScriptValuePtr ExecuteFunction(UScriptContextPtr context, const std::string &name, + ScriptParams *params); UScriptValuePtr FindVariable(UScriptContextPtr local, std::string id); UScriptValuePtr UpdateVariable(UScriptContextPtr local, std::string id, UScriptValuePtr var); int32_t GetInstanceId() const @@ -74,6 +75,7 @@ private: int32_t Execute(); private: + ScriptFunction* FindFunction(const std::string &name); UScriptStatementList* statements_ = nullptr; std::map functions_; std::vector contextStack_; @@ -83,4 +85,4 @@ private: int32_t instanceId_ = 0; }; } // namespace uscript -#endif \ No newline at end of file +#endif diff --git a/services/ui/drm_driver.cpp b/services/ui/drm_driver.cpp index 648719ac..20119428 100644 --- a/services/ui/drm_driver.cpp +++ b/services/ui/drm_driver.cpp @@ -19,7 +19,7 @@ #include "securec.h" namespace updater { -void DrmDriver::FlipBuffer(void *buf) +void DrmDriver::FlipBuffer(const void *buf) { if (!buf) { LOG(ERROR) << "buf is null"; diff --git a/services/ui/drm_driver.h b/services/ui/drm_driver.h index 62b5385f..1e074929 100644 --- a/services/ui/drm_driver.h +++ b/services/ui/drm_driver.h @@ -42,7 +42,7 @@ class DrmDriver { protected: DrmDriver() : fd_(-1), conn_(nullptr), res_(nullptr) {} virtual ~DrmDriver(); - void FlipBuffer(void* buf); + void FlipBuffer(const void* buf); void LoadDrmDriver(); private: int ModesetCreateFb(struct BufferObject *bo); @@ -51,7 +51,7 @@ private: int fd_; drmModeConnector *conn_; drmModeRes *res_; - struct BufferObject buff_; + struct BufferObject buff_ {}; }; } // namespace updater #endif diff --git a/services/ui/text_label.cpp b/services/ui/text_label.cpp index 0b87320e..d94a90a6 100644 --- a/services/ui/text_label.cpp +++ b/services/ui/text_label.cpp @@ -78,6 +78,15 @@ static void PngInitSet(png_structp fontPngPtr, FILE *fp, int size, png_infop fon return; } +static void CheckInitFont(png_structp fontPngPtr, FILE *fp, png_infop fontInfoPtr) +{ + png_destroy_read_struct(&fontPngPtr, &fontInfoPtr, 0); + if (fp != nullptr) { + fclose(fp); + fp = nullptr; + } +} + static void PNGReadRow(png_uint_32 fontWidth, png_uint_32 fontHeight, png_structp fontPngPtr, char *fontBuf) { if ((fontWidth > MAX_FONT_BUFFER_SIZE_HW) || (fontHeight > MAX_FONT_BUFFER_SIZE_HW)) { @@ -97,12 +106,10 @@ void TextLabel::InitFont() png_infop fontInfoPtr = nullptr; png_uint_32 fontWidth = 0; png_uint_32 fontHeight = 0; - png_byte fontChannels = 0; png_structp fontPngPtr = nullptr; int fontBitDepth = 0; int fontColorType = 0; - uint32_t offset = 2; - UPDATER_CHECK_ONLY_RETURN(!memset_s(resPath, MAX_TEXT_SIZE + offset, 0, MAX_TEXT_SIZE + 1), return); + UPDATER_CHECK_ONLY_RETURN(!memset_s(resPath, MAX_TEXT_SIZE + offset_, 0, MAX_TEXT_SIZE + 1), return); switch (fontType_) { case DEFAULT_FONT: UPDATER_CHECK_ONLY_RETURN(snprintf_s(resPath, sizeof(resPath), sizeof(resPath) -1, "/resources/%s.png", @@ -115,8 +122,7 @@ void TextLabel::InitFont() } FILE* fp = fopen(resPath, "rb"); UPDATER_ERROR_CHECK(fp, "open font failed!", return); - const int headerNumber = 8; - uint8_t header[headerNumber]; + uint8_t header[headerNumber_]; size_t bytesRead = fread(header, 1, sizeof(header), fp); UPDATER_ERROR_CHECK(bytesRead == sizeof(header), "read header failed!", fclose(fp); return); if (png_sig_cmp(header, 0, sizeof(header))) { @@ -128,21 +134,21 @@ void TextLabel::InitFont() UPDATER_ERROR_CHECK(fontPngPtr, "creat font ptr_ failed!", fclose(fp); return); fontInfoPtr = png_create_info_struct(fontPngPtr); if (fontInfoPtr == nullptr) { + png_destroy_read_struct(&fontPngPtr, nullptr, nullptr); fclose(fp); return; } PngInitSet(fontPngPtr, fp, sizeof(header), fontInfoPtr); png_get_IHDR(fontPngPtr, fontInfoPtr, &fontWidth, &fontHeight, &fontBitDepth, &fontColorType, nullptr, nullptr, nullptr); - fontChannels = png_get_channels(fontPngPtr, fontInfoPtr); - const int defaultFontBitDepth = 8; - if (fontBitDepth <= defaultFontBitDepth && fontChannels == 1 && fontColorType == PNG_COLOR_TYPE_GRAY) { + png_byte fontChannels = png_get_channels(fontPngPtr, fontInfoPtr); + if (fontBitDepth <= defaultFontBitDepth_ && fontChannels == 1 && fontColorType == PNG_COLOR_TYPE_GRAY) { png_set_expand_gray_1_2_4_to_8(fontPngPtr); } - const int defaultFontWidth = 96; - fontWidth_ = fontWidth / defaultFontWidth; + fontWidth_ = fontWidth / defaultFontWidth_; fontHeight_ = fontHeight >> 1; PNGReadRow(fontWidth_, fontHeight_, fontPngPtr, fontBuf_); + CheckInitFont(fontPngPtr, fp, fontInfoPtr); } void TextLabel::SetText(const char *str) diff --git a/services/ui/text_label.h b/services/ui/text_label.h index 39f4d1b2..82d5fb9a 100644 --- a/services/ui/text_label.h +++ b/services/ui/text_label.h @@ -75,6 +75,10 @@ private: char fontBuf_[MAX_FONT_BUFFER_SIZE_HW * FONT_BUFFER_SIZE] {}; unsigned int fontWidth_ = 0; unsigned int fontHeight_ = 0; + uint32_t offset_ = 2; + const int defaultFontWidth_ = 96; + const int defaultFontBitDepth_ = 8; + const int headerNumber_ = 8; }; } // namespace updater #endif diff --git a/services/updater.cpp b/services/updater.cpp index 43fb6b6f..f5e66db3 100644 --- a/services/updater.cpp +++ b/services/updater.cpp @@ -122,11 +122,12 @@ static UpdaterStatus IsSpaceCapacitySufficient(const std::string &packagePath) UPDATER_ERROR_CHECK(pkgManager != nullptr, "pkgManager is nullptr", return UPDATE_CORRUPT); std::vector fileIds; int ret = pkgManager->LoadPackageWithoutUnPack(packagePath, fileIds); - UPDATER_ERROR_CHECK(ret == PKG_SUCCESS, "LoadPackageWithoutUnPack failed", return UPDATE_CORRUPT); + UPDATER_ERROR_CHECK(ret == PKG_SUCCESS, "LoadPackageWithoutUnPack failed", + PkgManager::ReleasePackageInstance(pkgManager); return UPDATE_CORRUPT); const FileInfo *info = pkgManager->GetFileInfo("update.bin"); - UPDATER_ERROR_CHECK(info != nullptr, "update.bin is not exist", return UPDATE_CORRUPT); - + UPDATER_ERROR_CHECK(info != nullptr, "update.bin is not exist", + PkgManager::ReleasePackageInstance(pkgManager); return UPDATE_CORRUPT); PkgManager::ReleasePackageInstance(pkgManager); struct statvfs64 updaterVfs; @@ -331,7 +332,7 @@ UpdaterStatus StartUpdaterProc(PkgManager::PkgManagerPtr pkgManager, const std:: UPDATER_ERROR_CHECK(fromChild != nullptr, "fdopen pipeRead failed", return UPDATE_ERROR); while (fgets(buffer, MAX_BUFFER_SIZE - 1, fromChild) != nullptr) { size_t n = strlen(buffer); - if (buffer[n - 1] == '\n') { + if (n > 0 && buffer[n - 1] == '\n') { buffer[n - 1] = '\0'; } HandleChildOutput(buffer, MAX_BUFFER_SIZE, retryUpdate); diff --git a/services/updater_binary/update_partitions.cpp b/services/updater_binary/update_partitions.cpp index 81f26e8b..0f431deb 100644 --- a/services/updater_binary/update_partitions.cpp +++ b/services/updater_binary/update_partitions.cpp @@ -48,24 +48,25 @@ int UpdatePartitions::ParsePartitionInfo(const std::string &partitionInfo, Parti return 0; } cJSON* thisPartition = cJSON_GetArrayItem(partitions, i); - UPDATER_ERROR_CHECK(thisPartition != nullptr, "Error get thisPartion", free(myPartition); break); + UPDATER_ERROR_CHECK(thisPartition != nullptr, "Error get thisPartion", free(myPartition); + myPartition = nullptr; break); cJSON* item = cJSON_GetObjectItem(thisPartition, "start"); - UPDATER_ERROR_CHECK(item != nullptr, "Error get start", free(myPartition); break); + UPDATER_ERROR_CHECK(item != nullptr, "Error get start", free(myPartition); myPartition = nullptr; break); myPartition->start = item->valueint; item = cJSON_GetObjectItem(thisPartition, "length"); - UPDATER_ERROR_CHECK(item != nullptr, "Error get length", free(myPartition); break); + UPDATER_ERROR_CHECK(item != nullptr, "Error get length", free(myPartition); myPartition = nullptr; break); myPartition->length = item->valueint; myPartition->partNum = 0; myPartition->devName = "mmcblk0px"; item = cJSON_GetObjectItem(thisPartition, "partName"); - UPDATER_ERROR_CHECK(item != nullptr, "Error get partName", free(myPartition); break); + UPDATER_ERROR_CHECK(item != nullptr, "Error get partName", free(myPartition); myPartition = nullptr; break); myPartition->partName = (item->valuestring); item = cJSON_GetObjectItem(thisPartition, "fsType"); - UPDATER_ERROR_CHECK(item != nullptr, "Error get fsType", free(myPartition); break); + UPDATER_ERROR_CHECK(item != nullptr, "Error get fsType", free(myPartition); myPartition = nullptr; break); myPartition->fsType = (item->valuestring); LOG(INFO) << " "; diff --git a/utils/utils.cpp b/utils/utils.cpp index 52c0b201..84b21d20 100644 --- a/utils/utils.cpp +++ b/utils/utils.cpp @@ -162,17 +162,18 @@ std::string ConvertSha256Hex(const uint8_t* shaDigest, size_t length) void DoReboot(const std::string& rebootTarget) { LOG(INFO) << ", rebootTarget: " << rebootTarget; - static const int32_t maxCommandSize = 16; LoadFstab(); auto miscBlockDevice = GetBlockDeviceByMountPoint("/misc"); struct UpdateMessage msg; if (rebootTarget == "updater") { + std::string command = "boot_updater"; bool ret = ReadUpdaterMessage(miscBlockDevice, msg); UPDATER_ERROR_CHECK(ret == true, "DoReboot read misc failed", return); - if (strcmp(msg.command, "boot_updater") != 0) { - UPDATER_ERROR_CHECK(!memcpy_s(msg.command, maxCommandSize, "boot_updater", maxCommandSize - 1), + if (strcmp(msg.command, command.c_str()) != 0) { + UPDATER_ERROR_CHECK(memset_s(msg.command, MAX_COMMAND_SIZE, 0, MAX_COMMAND_SIZE) == 0, + "Failed to clear update message", return); + UPDATER_ERROR_CHECK(!memcpy_s(msg.command, MAX_COMMAND_SIZE - 1, command.c_str(), command.size()), "Memcpy failed", return); - msg.command[maxCommandSize] = 0; } ret = WriteUpdaterMessage(miscBlockDevice, msg); if (ret != true) { -- Gitee From 06091d2a3c4d614ebb152721417c63ae7fb80071 Mon Sep 17 00:00:00 2001 From: ma_nan Date: Fri, 24 Sep 2021 15:42:00 +0800 Subject: [PATCH 2/4] fix codestyle Signed-off-by: ma_nan --- services/diffpatch/bzip2/zip_adapter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/diffpatch/bzip2/zip_adapter.h b/services/diffpatch/bzip2/zip_adapter.h index 4385bf19..090bddef 100644 --- a/services/diffpatch/bzip2/zip_adapter.h +++ b/services/diffpatch/bzip2/zip_adapter.h @@ -40,7 +40,7 @@ public: private: std::vector buffer_ {}; UpdatePatchWriterPtr outStream_; - z_stream zstream_; + z_stream zstream_ {0}; size_t offset_; int32_t level_ {0}; -- Gitee From 020ce03bf8aaf65b1a791957e30c709d0e502eac Mon Sep 17 00:00:00 2001 From: ma_nan Date: Fri, 24 Sep 2021 17:06:20 +0800 Subject: [PATCH 3/4] fix codestyle Signed-off-by: ma_nan --- services/include/applypatch/partition_record.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/include/applypatch/partition_record.h b/services/include/applypatch/partition_record.h index 916863f4..962d43f4 100644 --- a/services/include/applypatch/partition_record.h +++ b/services/include/applypatch/partition_record.h @@ -68,11 +68,11 @@ private: private: std::string GetMiscPartitionPath(const std::string &mountPoint = "/misc"); - PartitionRecordInfo info_; + PartitionRecordInfo info_ {}; // offset of partition record in misc. // offset is not start from zero, but // start from the global offset of misc partition. - size_t offset_; + off_t offset_; }; } // namespace updater -#endif // UPDATER_PARTITION_UPDATE_RECORD_H \ No newline at end of file +#endif // UPDATER_PARTITION_UPDATE_RECORD_H -- Gitee From 3b405ea5e90311fb02734b2b8e46a0a0b606076c Mon Sep 17 00:00:00 2001 From: ma_nan Date: Fri, 24 Sep 2021 17:41:58 +0800 Subject: [PATCH 4/4] fix codestyle Signed-off-by: ma_nan --- services/applypatch/transfer_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/applypatch/transfer_manager.cpp b/services/applypatch/transfer_manager.cpp index bb2746c1..8cfd67c2 100644 --- a/services/applypatch/transfer_manager.cpp +++ b/services/applypatch/transfer_manager.cpp @@ -82,7 +82,7 @@ bool TransferManager::CommandsParser(int fd, const std::vector &con cmd->SetFileDescriptor(fd); std::unique_ptr cf = CommandFunctionFactory::GetCommandFunction(cmd->GetCommandType()); UPDATER_ERROR_CHECK(cf != nullptr, "Failed to get cmd exec", return false); - CommandResult ret = cf->Execute(const_cast(*cmd.get())); + CommandResult ret = cf->Execute(const_cast(*cmd.get())); CommandFunctionFactory::ReleaseCommandFunction(cf); if (CheckResult(ret, cmd->GetCommandLine(), cmd->GetCommandType()) == false) { return false; -- Gitee