diff options
author | saturneric <[email protected]> | 2023-12-15 09:04:59 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2023-12-15 09:04:59 +0000 |
commit | f5cf83e4b3fdf1e9ae82b00f39e45e189809c419 (patch) | |
tree | cc7d9b764b0274cfce5830e22a1ecc23678bd091 /src/core | |
parent | fix: slove issues on memory and add asan support for debug (diff) | |
download | GpgFrontend-f5cf83e4b3fdf1e9ae82b00f39e45e189809c419.tar.gz GpgFrontend-f5cf83e4b3fdf1e9ae82b00f39e45e189809c419.zip |
fix: slove some issues on memory and intilizations
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/function/GlobalSettingStation.cpp | 8 | ||||
-rw-r--r-- | src/core/function/SecureMemoryAllocator.cpp | 7 | ||||
-rw-r--r-- | src/core/function/SecureMemoryAllocator.h | 6 | ||||
-rw-r--r-- | src/core/function/basic/ChannelObject.cpp | 11 | ||||
-rw-r--r-- | src/core/function/basic/ChannelObject.h | 3 | ||||
-rw-r--r-- | src/core/function/basic/GpgFunctionObject.h | 3 | ||||
-rw-r--r-- | src/core/function/gpg/GpgContext.cpp | 6 | ||||
-rw-r--r-- | src/core/function/gpg/GpgKeyImportExporter.h | 2 | ||||
-rw-r--r-- | src/core/function/gpg/GpgKeyManager.cpp | 5 | ||||
-rw-r--r-- | src/core/utils/GpgUtils.cpp | 37 | ||||
-rw-r--r-- | src/core/utils/LogUtils.cpp | 33 | ||||
-rw-r--r-- | src/core/utils/LogUtils.h | 31 | ||||
-rw-r--r-- | src/core/utils/MemoryUtils.h | 12 |
13 files changed, 125 insertions, 39 deletions
diff --git a/src/core/function/GlobalSettingStation.cpp b/src/core/function/GlobalSettingStation.cpp index 5031effe..d1c1068e 100644 --- a/src/core/function/GlobalSettingStation.cpp +++ b/src/core/function/GlobalSettingStation.cpp @@ -28,7 +28,8 @@ #include "GlobalSettingStation.h" -#include <qcoreapplication.h> +#include <boost/dll.hpp> +#include <filesystem> #include "core/utils/FilesystemUtils.h" #include "core/utils/IOUtils.h" @@ -216,8 +217,9 @@ class GlobalSettingStation::Impl } private: - std::filesystem::path app_path_ = QCoreApplication::applicationDirPath() - .toStdString(); ///< Program Location + std::filesystem::path app_path_ = + std::filesystem::path(boost::dll::program_location().string()) + .parent_path(); std::filesystem::path app_data_path_ = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) diff --git a/src/core/function/SecureMemoryAllocator.cpp b/src/core/function/SecureMemoryAllocator.cpp index b9201942..2c584753 100644 --- a/src/core/function/SecureMemoryAllocator.cpp +++ b/src/core/function/SecureMemoryAllocator.cpp @@ -48,9 +48,6 @@ auto SecureMemoryAllocator::Reallocate(void* ptr, std::size_t size) -> void* { return addr; } -void SecureMemoryAllocator::Deallocate(void* p) { - SPDLOG_TRACE("secure memory allocator trys to free memory, address: {}", - static_cast<void*>(p)); - mi_free(p); -} +void SecureMemoryAllocator::Deallocate(void* p) { mi_free(p); } + } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/SecureMemoryAllocator.h b/src/core/function/SecureMemoryAllocator.h index b938429e..e9f1c1c3 100644 --- a/src/core/function/SecureMemoryAllocator.h +++ b/src/core/function/SecureMemoryAllocator.h @@ -31,6 +31,8 @@ #include <cstdint> #include <memory> +#include "core/utils/LogUtils.h" + namespace GpgFrontend { class GPGFRONTEND_CORE_EXPORT SecureMemoryAllocator { @@ -46,10 +48,6 @@ template <typename T> struct SecureObjectDeleter { void operator()(T *ptr) { if (ptr) { - SPDLOG_TRACE( - "secure object deleter trys to deconstruct and free object, " - "type: {}, addr: {}", - typeid(T).name(), static_cast<void *>(ptr)); ptr->~T(); SecureMemoryAllocator::Deallocate(ptr); } diff --git a/src/core/function/basic/ChannelObject.cpp b/src/core/function/basic/ChannelObject.cpp index 7a41d4d1..3f040ca6 100644 --- a/src/core/function/basic/ChannelObject.cpp +++ b/src/core/function/basic/ChannelObject.cpp @@ -28,13 +28,20 @@ #include "ChannelObject.h" +#include <ostream> +#include <utility> +#include <iostream> + namespace GpgFrontend { ChannelObject::ChannelObject() noexcept = default; -ChannelObject::ChannelObject(int channel) : channel_(channel) {} +ChannelObject::ChannelObject(int channel, std::string type) + : channel_(channel), type_(std::move(type)) {} -ChannelObject::~ChannelObject() noexcept = default; +ChannelObject::~ChannelObject() noexcept { + std::cout << "deleting channel object: " << type_ << std::endl; +} void ChannelObject::SetChannel(int channel) { this->channel_ = channel; } diff --git a/src/core/function/basic/ChannelObject.h b/src/core/function/basic/ChannelObject.h index 6fb315ae..18317ee2 100644 --- a/src/core/function/basic/ChannelObject.h +++ b/src/core/function/basic/ChannelObject.h @@ -55,7 +55,7 @@ class GPGFRONTEND_CORE_EXPORT ChannelObject { * * @param channel */ - explicit ChannelObject(int channel); + explicit ChannelObject(int channel, std::string type); /** * @brief Get the Default Channel object @@ -80,6 +80,7 @@ class GPGFRONTEND_CORE_EXPORT ChannelObject { private: int channel_ = kGpgFrontendDefaultChannel; ///< The channel id + std::string type_; }; template <typename Derived> diff --git a/src/core/function/basic/GpgFunctionObject.h b/src/core/function/basic/GpgFunctionObject.h index dd06608d..afe81131 100644 --- a/src/core/function/basic/GpgFunctionObject.h +++ b/src/core/function/basic/GpgFunctionObject.h @@ -216,7 +216,8 @@ class SingletonFunctionObject : public ChannelObject { * * @param channel */ - explicit SingletonFunctionObject(int channel) : ChannelObject(channel) {} + explicit SingletonFunctionObject(int channel) + : ChannelObject(channel, typeid(T).name()) {} /** * @brief Destroy the Singleton Function Object object diff --git a/src/core/function/gpg/GpgContext.cpp b/src/core/function/gpg/GpgContext.cpp index 0477b00a..443c139b 100644 --- a/src/core/function/gpg/GpgContext.cpp +++ b/src/core/function/gpg/GpgContext.cpp @@ -32,12 +32,12 @@ #include <gpgme.h> #include <cassert> +#include <mutex> #include "core/function/CoreSignalStation.h" #include "core/function/basic/GpgFunctionObject.h" #include "core/module/ModuleManager.h" #include "core/utils/GpgUtils.h" -#include "spdlog/spdlog.h" #include "utils/MemoryUtils.h" #ifdef _WIN32 @@ -158,6 +158,8 @@ class GpgContext::Impl : public SingletonFunctionObject<GpgContext::Impl> { gpgme_ctx_t ctx_ref_ = nullptr; ///< gpgme_ctx_t binary_ctx_ref_ = nullptr; ///< bool good_ = true; + std::mutex ctx_ref_lock_; + std::mutex binary_ctx_ref_lock_; static auto set_ctx_key_list_mode(const gpgme_ctx_t &ctx) -> bool { assert(ctx != nullptr); @@ -205,6 +207,8 @@ class GpgContext::Impl : public SingletonFunctionObject<GpgContext::Impl> { assert(CheckGpgError(err) == GPG_ERR_NO_ERROR); return CheckGpgError(err) == GPG_ERR_NO_ERROR; + + return true; } auto common_ctx_initialize(const gpgme_ctx_t &ctx, diff --git a/src/core/function/gpg/GpgKeyImportExporter.h b/src/core/function/gpg/GpgKeyImportExporter.h index 20eacd71..3c88c2c5 100644 --- a/src/core/function/gpg/GpgKeyImportExporter.h +++ b/src/core/function/gpg/GpgKeyImportExporter.h @@ -100,7 +100,7 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyImportExporter * @param inBuffer * @return GpgImportInformation */ - auto ImportKey(StdBypeArrayPtr inBuffer) -> GpgImportInformation; + auto ImportKey(StdBypeArrayPtr) -> GpgImportInformation; /** * @brief diff --git a/src/core/function/gpg/GpgKeyManager.cpp b/src/core/function/gpg/GpgKeyManager.cpp index 4a67a3f4..7afc356a 100644 --- a/src/core/function/gpg/GpgKeyManager.cpp +++ b/src/core/function/gpg/GpgKeyManager.cpp @@ -184,11 +184,6 @@ auto GpgFrontend::GpgKeyManager::SetOwnerTrustLevel(const GpgKey& key, auto err = gpgme_op_interact( ctx_.DefaultContext(), static_cast<gpgme_key_t>(key), 0, GpgKeyManager::interactor_cb_fnc, (void*)&handel_struct, data_out); - if (err != GPG_ERR_NO_ERROR) { - SPDLOG_ERROR("fail to set owner trust level {} to key {}, err: {}", - trust_level, key.GetId(), gpgme_strerror(err)); - } - return CheckGpgError(err) == GPG_ERR_NO_ERROR && handel_struct.Success(); } diff --git a/src/core/utils/GpgUtils.cpp b/src/core/utils/GpgUtils.cpp index 8588f117..ba8d8ba8 100644 --- a/src/core/utils/GpgUtils.cpp +++ b/src/core/utils/GpgUtils.cpp @@ -53,11 +53,26 @@ static inline auto Trim(std::string& s) -> std::string { return s; } +auto GetGpgmeErrorString(size_t buffer_size, gpgme_error_t err) -> std::string { + std::vector<char> buffer(buffer_size); + + gpgme_error_t ret = gpgme_strerror_r(err, buffer.data(), buffer.size()); + if (ret == ERANGE && buffer_size < 1024) { + return GetGpgmeErrorString(buffer_size * 2, err); + } + + return std::string(buffer.data()); +} + +auto GetGpgmeErrorString(gpgme_error_t err) -> std::string { + return GetGpgmeErrorString(64, err); +} + auto CheckGpgError(GpgError err) -> GpgError { if (gpg_err_code(err) != GPG_ERR_NO_ERROR) { SPDLOG_ERROR( "gpg operation failed [error code: {}], source: {} description: {}", - gpg_err_code(err), gpgme_strsource(err), gpgme_strerror(err)); + gpg_err_code(err), gpgme_strsource(err), GetGpgmeErrorString(err)); } return err; } @@ -65,27 +80,27 @@ auto CheckGpgError(GpgError err) -> GpgError { auto CheckGpgError2ErrCode(GpgError err, GpgError predict) -> GpgErrorCode { auto err_code = gpg_err_code(err); if (err_code != gpg_err_code(predict)) { - if (err_code == GPG_ERR_NO_ERROR) + if (err_code == GPG_ERR_NO_ERROR) { SPDLOG_WARN("[Warning {}] Source: {} description: {} predict: {}", - gpg_err_code(err), gpgme_strsource(err), gpgme_strerror(err), - gpgme_strerror(err)); - else + gpg_err_code(err), gpgme_strsource(err), + GetGpgmeErrorString(err), GetGpgmeErrorString(predict)); + } else { SPDLOG_ERROR("[Error {}] Source: {} description: {} predict: {}", - gpg_err_code(err), gpgme_strsource(err), gpgme_strerror(err), - gpgme_strerror(err)); + gpg_err_code(err), gpgme_strsource(err), + GetGpgmeErrorString(err), GetGpgmeErrorString(predict)); + } } return err_code; } auto DescribeGpgErrCode(GpgError err) -> GpgErrorDesc { - return {gpgme_strsource(err), gpgme_strerror(err)}; + return {gpgme_strsource(err), GetGpgmeErrorString(err)}; } auto CheckGpgError(GpgError err, const std::string& /*comment*/) -> GpgError { if (gpg_err_code(err) != GPG_ERR_NO_ERROR) { - SPDLOG_WARN("[Error {}] Source: {} description: {} predict: {}", - gpg_err_code(err), gpgme_strsource(err), gpgme_strerror(err), - gpgme_strerror(err)); + SPDLOG_WARN("[Error {}] Source: {} description: {}", gpg_err_code(err), + gpgme_strsource(err), GetGpgmeErrorString(err)); } return err; } diff --git a/src/core/utils/LogUtils.cpp b/src/core/utils/LogUtils.cpp new file mode 100644 index 00000000..d7d13579 --- /dev/null +++ b/src/core/utils/LogUtils.cpp @@ -0,0 +1,33 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "LogUtils.h" + +auto GetCoreLogger() -> std::shared_ptr<spdlog::logger> { + return spdlog::get("core"); +}
\ No newline at end of file diff --git a/src/core/utils/LogUtils.h b/src/core/utils/LogUtils.h new file mode 100644 index 00000000..a4d29dd8 --- /dev/null +++ b/src/core/utils/LogUtils.h @@ -0,0 +1,31 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#pragma once + +auto GPGFRONTEND_CORE_EXPORT GetCoreLogger() -> std::shared_ptr<spdlog::logger>;
\ No newline at end of file diff --git a/src/core/utils/MemoryUtils.h b/src/core/utils/MemoryUtils.h index 4c68e864..5f4283b2 100644 --- a/src/core/utils/MemoryUtils.h +++ b/src/core/utils/MemoryUtils.h @@ -30,7 +30,6 @@ #include "core/GpgFrontendCoreExport.h" #include "core/function/SecureMemoryAllocator.h" -#include "spdlog/spdlog.h" /* To avoid that a compiler optimizes certain memset calls away, these macros may be used instead. */ @@ -112,6 +111,7 @@ template <typename T, typename... Args> static auto SecureCreateObject(Args &&...args) -> T * { void *mem = SecureMemoryAllocator::Allocate(sizeof(T)); if (!mem) return nullptr; + SPDLOG_TRACE("alloc secure memnory success, type: {}, size: {}, addr: {}", typeid(T).name(), sizeof(T), mem); @@ -126,8 +126,6 @@ static auto SecureCreateObject(Args &&...args) -> T * { template <typename T> static void SecureDestroyObject(T *obj) { if (!obj) return; - - SPDLOG_TRACE("try to free object, obj: {}", static_cast<void *>(obj)); obj->~T(); SecureMemoryAllocator::Deallocate(obj); } @@ -137,8 +135,10 @@ static auto SecureCreateUniqueObject(Args &&...args) -> std::unique_ptr<T, SecureObjectDeleter<T>> { void *mem = SecureMemoryAllocator::Allocate(sizeof(T)); if (!mem) throw std::bad_alloc(); + SPDLOG_TRACE( - "alloc secure memnory success, unique ptr, type: {}, size: {}, addr: {}", + "alloc secure memnory success, unique ptr, " + "type: {}, size: {}, addr: {}", typeid(T).name(), sizeof(T), mem); try { @@ -154,8 +154,10 @@ template <typename T, typename... Args> auto SecureCreateSharedObject(Args &&...args) -> std::shared_ptr<T> { void *mem = SecureMemoryAllocator::Allocate(sizeof(T)); if (!mem) throw std::bad_alloc(); + SPDLOG_TRACE( - "alloc secure memnory success, shared ptr, type: {}, size: {}, addr: {}", + "alloc secure memnory success, shared ptr, " + "type: {}, size: {}, addr: {}", typeid(T).name(), sizeof(T), mem); try { |