From 077021749fe83765ab2b7b844838188f1eec4066 Mon Sep 17 00:00:00 2001 From: Amin Ya Date: Sat, 29 Aug 2026 04:55:23 -0700 Subject: [PATCH] fix: avoid null logger dereference in exceptions This fixes the spdlog logger null pointer deference when an exception is thrown. The logger is initialized before all paths now --- src/main.cpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index bd9fb40..7616e15 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -161,6 +161,23 @@ auto main(int argc, char** argv) -> std::int32_t { return EXIT_FAILURE; } + const auto& cache_path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation); + const auto& log_filepath = fmt::format("{}/cachyospi.log", cache_path.toStdString()); + if (fs::exists(log_filepath)) { + std::ifstream currentfile{log_filepath}; + const std::string file_data{std::istreambuf_iterator(currentfile), std::istreambuf_iterator()}; + std::ofstream oldlogfile{fmt::format("{}.old", log_filepath)}; + oldlogfile << "-----------------------------------------------------------\nCACHYOSPI SESSION\n" + "-----------------------------------------------------------\n"; + oldlogfile << file_data; + fs::remove(log_filepath); + } + auto logger = spdlog::create_async("cachyos_logger", log_filepath); + spdlog::set_default_logger(logger); + spdlog::set_pattern("[%r][%^---%L---%$] %v"); + spdlog::set_level(spdlog::level::debug); + spdlog::flush_every(std::chrono::seconds(5)); + // Check if we have valid databases { if (!alpm::is_valid_alpm_dbs()) { @@ -194,23 +211,6 @@ auto main(int argc, char** argv) -> std::int32_t { return EXIT_FAILURE; } - const auto& cache_path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation); - const auto& log_filepath = fmt::format("{}/cachyospi.log", cache_path.toStdString()); - if (fs::exists(log_filepath)) { - std::ifstream currentfile{log_filepath}; - const std::string file_data{std::istreambuf_iterator(currentfile), std::istreambuf_iterator()}; - std::ofstream oldlogfile{fmt::format("{}.old", log_filepath)}; - oldlogfile << "-----------------------------------------------------------\nCACHYOSPI SESSION\n" - "-----------------------------------------------------------\n"; - oldlogfile << file_data; - fs::remove(log_filepath); - } - auto logger = spdlog::create_async("cachyos_logger", log_filepath); - spdlog::set_default_logger(logger); - spdlog::set_pattern("[%r][%^---%L---%$] %v"); - spdlog::set_level(spdlog::level::debug); - spdlog::flush_every(std::chrono::seconds(5)); - MainWindow w; w.show(); const auto& status_code = QApplication::exec();