From 980e4cad0679b115ec633bad0098ea259b50020a Mon Sep 17 00:00:00 2001 From: iTrooz Date: Fri, 5 May 2023 21:57:37 +0200 Subject: [PATCH] fix: Handle errors in Tar::readVector() (#1059) --- lib/libimhex/include/hex/helpers/tar.hpp | 1 + lib/libimhex/source/helpers/tar.cpp | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/tar.hpp b/lib/libimhex/include/hex/helpers/tar.hpp index 888000bfb..2613449fd 100644 --- a/lib/libimhex/include/hex/helpers/tar.hpp +++ b/lib/libimhex/include/hex/helpers/tar.hpp @@ -42,6 +42,7 @@ namespace hex { private: mtar_t m_ctx = { }; + std::fs::path m_path; bool m_valid = false; }; diff --git a/lib/libimhex/source/helpers/tar.cpp b/lib/libimhex/source/helpers/tar.cpp index 58776929f..d76efe988 100644 --- a/lib/libimhex/source/helpers/tar.cpp +++ b/lib/libimhex/source/helpers/tar.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include @@ -27,6 +28,7 @@ namespace hex { else error = MTAR_EFAILURE; + this->m_path = path; this->m_valid = (error == MTAR_ESUCCESS); } @@ -36,6 +38,7 @@ namespace hex { Tar::Tar(hex::Tar &&other) noexcept { this->m_ctx = other.m_ctx; + this->m_path = other.m_path; this->m_valid = other.m_valid; other.m_ctx = { }; @@ -46,6 +49,8 @@ namespace hex { this->m_ctx = other.m_ctx; other.m_ctx = { }; + this->m_path = other.m_path; + this->m_valid = other.m_valid; other.m_valid = false; @@ -91,8 +96,13 @@ namespace hex { #if defined(OS_WINDOWS) std::replace(fixedPath.begin(), fixedPath.end(), '\\', '/'); #endif - mtar_find(&this->m_ctx, fixedPath.c_str(), &header); - + int ret = mtar_find(&this->m_ctx, fixedPath.c_str(), &header); + if(ret != MTAR_ESUCCESS){ + log::debug("Failed to read vector from path {} in tarred file {}: {}", + path.string(), this->m_path.string(), mtar_strerror(ret)); + return {}; + } + std::vector result(header.size, 0x00); mtar_read_data(&this->m_ctx, result.data(), result.size());