Skip to content

Commit

Permalink
Copr plugin: Fix resource leak in load_all_configuration
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
jrohel committed Jan 6, 2025
1 parent 8751434 commit 2201238
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
25 changes: 19 additions & 6 deletions dnf5-plugins/copr_plugin/copr_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "libdnf5/utils/os_release.hpp"

#include <cstdlib>
#include <filesystem>
#include <set>


namespace dnf5 {
Expand All @@ -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<std::filesystem::path> 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:
Expand Down
1 change: 0 additions & 1 deletion dnf5-plugins/copr_plugin/copr_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#ifndef DNF5_COMMANDS_COPR_COPR_CONFIG_HPP
#define DNF5_COMMANDS_COPR_COPR_CONFIG_HPP

#include <glob.h>
#include <libdnf5/base/base.hpp>
#include <libdnf5/conf/config_parser.hpp>

Expand Down

0 comments on commit 2201238

Please sign in to comment.