From fa0a622f50d486a3a1488af3409f634666650794 Mon Sep 17 00:00:00 2001 From: Peguen <73380451+Peguen@users.noreply.github.com> Date: Tue, 5 Nov 2024 10:33:57 +0100 Subject: [PATCH] Changed cwdPath logic to not leak memory. --- .../src/config/ecal_config_initializer.cpp | 34 ++++++++++++------- 1 file changed, 22 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..0c601c76b7 100644 --- a/ecal/core/src/config/ecal_config_initializer.cpp +++ b/ecal/core/src/config/ecal_config_initializer.cpp @@ -34,17 +34,22 @@ #include "configuration_to_yaml.h" #endif + // for cwd #ifdef ECAL_OS_WINDOWS + #include #include // to remove deprecated warning #define getcwd _getcwd + constexpr int MAXIMUM_PATH_LENGTH = _MAX_PATH; #endif #ifdef ECAL_OS_LINUX #include #include #include #include + #include + constexpr int MAXIMUM_PATH_LENGTH = PATH_MAX; #endif #include "ecal_utils/filesystem.h" @@ -67,16 +72,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 +92,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()