diff --git a/libdnf5/rpm/rpm_log_guard.cpp b/libdnf5/rpm/rpm_log_guard.cpp index 2a7d48061..28bca3e90 100644 --- a/libdnf5/rpm/rpm_log_guard.cpp +++ b/libdnf5/rpm/rpm_log_guard.cpp @@ -65,6 +65,16 @@ static int rpmlog_callback(rpmlogRec rec, rpmlogCallbackData data) { return 0; } +RpmLogGuardBase::~RpmLogGuardBase() { + // Reset the RPM log callback to nullptr to prevent undefined behavior if the + // callback data retains a reference to the RpmLogGuard object after it has + // been destroyed. + // The class doesn't reset the original callback upon destruction, because + // although `rpmlogSetCallback()` returns the old callback, there's no way + // to also retrieve the `rpmlogCallbackData` associated with it, so the + // data can't be reset. + rpmlogSetCallback(nullptr, nullptr); +} RpmLogGuard::RpmLogGuard(const BaseWeakPtr & base) : RpmLogGuardBase(), base(base) { rpmlogSetCallback(&rpmlog_callback, this); diff --git a/libdnf5/rpm/rpm_log_guard.hpp b/libdnf5/rpm/rpm_log_guard.hpp index 0a0e7233e..11cc4fbeb 100644 --- a/libdnf5/rpm/rpm_log_guard.hpp +++ b/libdnf5/rpm/rpm_log_guard.hpp @@ -31,7 +31,7 @@ namespace libdnf5::rpm { class RpmLogGuardBase { public: RpmLogGuardBase() : rpm_log_mutex_guard(rpm_log_mutex) {} - ~RpmLogGuardBase() {} + virtual ~RpmLogGuardBase(); private: static std::mutex rpm_log_mutex;