From 47070ab706f4ac9b3668fc56a2108f6cf2d03f06 Mon Sep 17 00:00:00 2001 From: Jaroslav Rohel Date: Mon, 6 Jan 2025 10:37:43 +0100 Subject: [PATCH] Copr plugin: Fix resource leak in load_all_configuration The "globfree" function call was missing. One can say that adding the line `globfree(&glob_result);` after the for loop is enough. But it's not true. The `load_copr_config_file` method called in the loop can generate an exception. So I rewrote the code to use `std::filesystem::directory_iterator`. --- dnf5-plugins/copr_plugin/copr_config.cpp | 25 ++++++++++++++++++------ dnf5-plugins/copr_plugin/copr_config.hpp | 1 - 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/dnf5-plugins/copr_plugin/copr_config.cpp b/dnf5-plugins/copr_plugin/copr_config.cpp index 483207f05..3e19c0335 100644 --- a/dnf5-plugins/copr_plugin/copr_config.cpp +++ b/dnf5-plugins/copr_plugin/copr_config.cpp @@ -24,7 +24,9 @@ along with libdnf. If not, see . #include "libdnf5/utils/os_release.hpp" +#include #include +#include namespace dnf5 { @@ -49,12 +51,23 @@ void CoprConfig::load_all_configuration() { load_copr_config_file("/usr/share/dnf/plugins/copr.vendor.conf"); load_copr_config_file(etc_dir / "dnf/plugins/copr.vendor.conf"); load_copr_config_file(etc_dir / "dnf/plugins/copr.conf"); - std::string pattern = etc_dir / "dnf/plugins/copr.d/*.conf"; - glob_t glob_result; - glob(pattern.c_str(), GLOB_MARK, nullptr, &glob_result); - for (size_t i = 0; i < glob_result.gl_pathc; ++i) { - std::string file_path = glob_result.gl_pathv[i]; - load_copr_config_file(file_path); + + // Load configuration files from drop-in directory + { + // Create a set of configuration files sorted by name from the drop-in directory + const auto drop_in_dir = etc_dir / "dnf/plugins/copr.d/"; + std::set drop_in_files; + std::error_code ec; + for (const auto & dentry : std::filesystem::directory_iterator(drop_in_dir, ec)) { + const auto & path = dentry.path(); + if (dentry.is_regular_file() && path.extension() == ".conf") { + drop_in_files.insert(path); + } + } + + for (const auto & path : drop_in_files) { + load_copr_config_file(path); + } } // For DNF4, we used to have: diff --git a/dnf5-plugins/copr_plugin/copr_config.hpp b/dnf5-plugins/copr_plugin/copr_config.hpp index 0fb5c27f1..dcd6d2d39 100644 --- a/dnf5-plugins/copr_plugin/copr_config.hpp +++ b/dnf5-plugins/copr_plugin/copr_config.hpp @@ -20,7 +20,6 @@ along with libdnf. If not, see . #ifndef DNF5_COMMANDS_COPR_COPR_CONFIG_HPP #define DNF5_COMMANDS_COPR_COPR_CONFIG_HPP -#include #include #include