From 0a47c890a11ec9dee0615accfc0c2cfbd29c8175 Mon Sep 17 00:00:00 2001 From: Peguen <73380451+Peguen@users.noreply.github.com> Date: Fri, 8 Nov 2024 10:58:47 +0100 Subject: [PATCH] [config] Changed cwdPath logic to not leak memory (#1781) --- .../src/config/ecal_config_initializer.cpp | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/ecal/core/src/config/ecal_config_initializer.cpp b/ecal/core/src/config/ecal_config_initializer.cpp index a3b154c2b2..4213725eee 100644 --- a/ecal/core/src/config/ecal_config_initializer.cpp +++ b/ecal/core/src/config/ecal_config_initializer.cpp @@ -34,17 +34,21 @@ #include "configuration_to_yaml.h" #endif +#include + // for cwd #ifdef ECAL_OS_WINDOWS #include // to remove deprecated warning #define getcwd _getcwd + constexpr int MAXIMUM_PATH_LENGTH = _MAX_PATH; #endif #ifdef ECAL_OS_LINUX #include #include #include #include + constexpr int MAXIMUM_PATH_LENGTH = PATH_MAX; #endif #include "ecal_utils/filesystem.h" @@ -67,16 +71,15 @@ namespace bool setPathSep(std::string& file_path_) { - if (!file_path_.empty()) + if (file_path_.empty()) + return false; + + if (file_path_.back() != path_separator) { - if (file_path_.back() != path_separator) - { - file_path_ += path_separator; - } - return true; + file_path_ += path_separator; } - - return false; + + return true; } std::string eCALDataEnvPath() @@ -88,10 +91,16 @@ namespace std::string cwdPath() { - std::string cwd_path = { getcwd(nullptr, 0) }; - - setPathSep(cwd_path); - return cwd_path; + char temp[MAXIMUM_PATH_LENGTH]; + + if (getcwd(temp, sizeof(temp)) != nullptr) + { + std::string cwdPath{temp}; + setPathSep(cwdPath); + return cwdPath; + } + + return {}; } std::string eCALDataCMakePath()