From 29690cdd21394619f793764f29075d01223d28c9 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Wed, 7 May 2025 09:54:52 -0400 Subject: [PATCH] MT#62181 AmPlugin: change to std::filesystem Fixes a resource leak. Change-Id: I263adc43aa5b3c5925cc37fc8bdc7fa3f3372337 Warned-by: Coverity --- core/AmPlugIn.cpp | 55 +++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/core/AmPlugIn.cpp b/core/AmPlugIn.cpp index 985f947c..f0e2be39 100644 --- a/core/AmPlugIn.cpp +++ b/core/AmPlugIn.cpp @@ -46,6 +46,7 @@ #include #include #include +#include using std::set; static unsigned int pcm16_bytes2samples(long h_codec, unsigned int num_bytes) @@ -180,44 +181,38 @@ int AmPlugIn::load(const string& directory, const string& plugins) if (!plugins.length()) { INFO("AmPlugIn: loading modules in directory '%s':\n", directory.c_str()); - DIR* dir = opendir(directory.c_str()); - if (!dir){ - ERROR("while opening plug-in directory (%s): %s\n", - directory.c_str(), strerror(errno)); - return -1; - } - vector excluded_plugins = explode(AmConfig::ExcludePlugins, ";"); set excluded_plugins_s; for (vector::iterator it = excluded_plugins.begin(); it != excluded_plugins.end();it++) excluded_plugins_s.insert(*it); - struct dirent* entry; - - while( ((entry = readdir(dir)) != NULL) && (err == 0) ){ - string plugin_name = string(entry->d_name); - - if(plugin_name.find(".so",plugin_name.length()-3) == string::npos ){ - continue; - } - - if (excluded_plugins_s.find(plugin_name.substr(0, plugin_name.length()-3)) - != excluded_plugins_s.end()) { - DBG("skipping excluded plugin %s\n", plugin_name.c_str()); - continue; - } - - string plugin_file = directory + "/" + plugin_name; - - DBG("loading %s ...\n",plugin_file.c_str()); - if( (err = loadPlugIn(plugin_file, plugin_name, loaded_plugins)) < 0 ) { - ERROR("while loading plug-in '%s'\n",plugin_file.c_str()); - return -1; + try { + for (const auto& entry : std::filesystem::directory_iterator(directory)) { + const auto& plugin_name = entry.path().filename().string(); + const auto& plugin_file = entry.path().string(); + + if(plugin_name.find(".so",plugin_name.length()-3) == string::npos ){ + continue; + } + + if (excluded_plugins_s.find(plugin_name.substr(0, plugin_name.length()-3)) + != excluded_plugins_s.end()) { + DBG("skipping excluded plugin %s\n", plugin_name.c_str()); + continue; + } + + DBG("loading %s ...\n",plugin_file.c_str()); + if( (err = loadPlugIn(plugin_file, plugin_name, loaded_plugins)) < 0 ) { + ERROR("while loading plug-in '%s'\n",plugin_file.c_str()); + return -1; + } } } - - closedir(dir); + catch (std::filesystem::filesystem_error& e) { + ERROR("while opening/reading plug-in directory (%s): %s\n", + directory.c_str(), e.what()); + } } else { INFO("AmPlugIn: loading modules: '%s'\n", plugins.c_str());