From b91f4f6c9339033a0799c127d9bf863a59c5fafb Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Wed, 12 Mar 2025 13:54:33 -0400 Subject: [PATCH] MT#59962 DSM: switch config file loading to STL Use std::filesystem instead of opendir/readdir. Fixes a reasource leak. Change-Id: Ifb897f15de1dac0233c50b3cd1e3e7fec4d3fffe Warned-by: Coverity --- apps/dsm/DSM.cpp | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/apps/dsm/DSM.cpp b/apps/dsm/DSM.cpp index a437e244..785ade87 100644 --- a/apps/dsm/DSM.cpp +++ b/apps/dsm/DSM.cpp @@ -41,9 +41,9 @@ #include #include +#include #include -#include #define MOD_NAME "dsm" @@ -235,29 +235,25 @@ int DSMFactory::onLoad() conf_d_dir += '/'; DBG("processing configurations in '%s'...\n", conf_d_dir.c_str()); - int err=0; - struct dirent* entry; - DIR* dir = opendir(conf_d_dir.c_str()); - - if(!dir){ - INFO("DSM config files loader (%s): %s\n", - conf_d_dir.c_str(), strerror(errno)); - } else { - while( ((entry = readdir(dir)) != NULL) && (err == 0) ){ - string conf_name = string(entry->d_name); - + + try { + for (const auto& entry : std::filesystem::directory_iterator(conf_d_dir)) { + const auto& conf_name = entry.path().filename().string(); + const auto& conf_file_name = entry.path().string(); + if (conf_name.find(".conf",conf_name.length()-5) == string::npos){ continue; } - string conf_file_name = conf_d_dir + conf_name; - DBG("loading %s ...\n",conf_file_name.c_str()); if (!loadConfig(conf_file_name, conf_name, false, NULL)) return -1; } - closedir(dir); + } + catch (std::filesystem::filesystem_error& e) { + INFO("DSM config files loader (%s): %s\n", + conf_d_dir.c_str(), e.what()); } }