From 6665a43d2b901b89809c4f18338d8ea6c0b08df7 Mon Sep 17 00:00:00 2001 From: Lefucjusz Date: Mon, 2 Oct 2023 14:00:46 +0200 Subject: [PATCH] [BH-1780] Fix uncaught std::filesystem::file_size exception Fix of the issue that exceptions thrown by file_size method were not caught at all, what resulted in system crashing. --- harmony_changelog.md | 1 + .../endpoints/update/UpdateHelper.cpp | 22 +++++++++++++------ pure_changelog.md | 1 + 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/harmony_changelog.md b/harmony_changelog.md index c9eabc6c4ef46102c3e52e4f64cd73635cdc16d6..711f14a81c3ce7b63dc2a36eb396efa7472c2dcf 100644 --- a/harmony_changelog.md +++ b/harmony_changelog.md @@ -9,6 +9,7 @@ * Fixed not disappearing snooze icon when an alarm is deactivated * Fixed incorrect message after new alarm setting in some scenarios * Fixed frequency lock during user activity +* Fixed possibility of OS crash during update package size check ### Added * Files not fully transferred via Center will be now removed when USB cable is unplugged diff --git a/module-services/service-desktop/endpoints/update/UpdateHelper.cpp b/module-services/service-desktop/endpoints/update/UpdateHelper.cpp index f3c426dfb18a46984449628d0c3f2ed910b379f3..912ea8b1702c5e042dcd15ed64dab5937a2f02cd 100644 --- a/module-services/service-desktop/endpoints/update/UpdateHelper.cpp +++ b/module-services/service-desktop/endpoints/update/UpdateHelper.cpp @@ -20,7 +20,10 @@ namespace sdesktop::endpoints { - constexpr auto chunkSize = 1024 * 128; + namespace + { + constexpr auto chunkSize = 1024 * 128; + } struct UpdatePackageEntries { @@ -56,7 +59,7 @@ namespace sdesktop::endpoints std::string err; const auto versionJson = json11::Json::parse(buffer.str(), err); if (!err.empty()) { - LOG_ERROR("Parsing '%s' failed", path.c_str()); + LOG_ERROR("Parsing '%s' failed, error message: '%s'", path.c_str(), err.c_str()); return std::nullopt; } return versionJson; @@ -85,7 +88,7 @@ namespace sdesktop::endpoints } std::fclose(fd); - return md5.getHash() == hash; + return (md5.getHash() == hash); } bool removeDirectory(const std::filesystem::path &path) @@ -112,7 +115,7 @@ namespace sdesktop::endpoints tar::unpack(path, where); } catch (const std::filesystem::filesystem_error &err) { - LOG_ERROR("Unpacking tar '%s' failed with %s", path.c_str(), err.what()); + LOG_ERROR("Unpacking tar '%s' failed with '%s'", path.c_str(), err.what()); return false; } return true; @@ -141,7 +144,13 @@ namespace sdesktop::endpoints return false; } - const auto requiredSpace = std::filesystem::file_size(updatePackage); + std::error_code fileSizeError; + const auto requiredSpace = std::filesystem::file_size(updatePackage, fileSizeError); + if (fileSizeError) { + LOG_ERROR("File size check failed, error message: '%s'", fileSizeError.message().c_str()); + return false; + } + const auto freeSpace = (static_cast(stat.f_bfree) * static_cast(stat.f_bsize)); LOG_INFO("Checking available space: %" PRIu64 " bytes, required: %" PRIu64 " bytes", freeSpace, @@ -156,7 +165,7 @@ namespace sdesktop::endpoints return std::filesystem::exists(path); } - void UpdateHelper::preProcess(http::Method method, Context &context) + void UpdateHelper::preProcess(http::Method method, [[maybe_unused]] Context &context) { LOG_INFO("In UpdateHelper - requesting %d", static_cast(method)); } @@ -197,5 +206,4 @@ namespace sdesktop::endpoints UpdateHelper::UpdateHelper(sys::Service *p) : BaseHelper(p), updatePackagePath{purefs::dir::getTemporaryPath() / "update"}, binariesPath{get_binary_dir()} {} - } // namespace sdesktop::endpoints diff --git a/pure_changelog.md b/pure_changelog.md index 6d620ba70870e4a5bc30f24f7693403f39347c58..2d2901c9afbd36a077a02a324ecdbd63162417f9 100644 --- a/pure_changelog.md +++ b/pure_changelog.md @@ -27,6 +27,7 @@ * Fixed alarm preview playback behavior * Fixed frequency lock during user activity * Fixed crash on transferring audio file with big metadata +* Fixed possibility of OS crash during update package size check ## [1.8.0 2023-09-27]