Skip to content

Commit

Permalink
[config] Changed cwdPath logic to not leak memory (#1781)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peguen authored Nov 8, 2024
1 parent 066471a commit 0a47c89
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions ecal/core/src/config/ecal_config_initializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,21 @@
#include "configuration_to_yaml.h"
#endif

#include <climits>

// for cwd
#ifdef ECAL_OS_WINDOWS
#include <direct.h>
// to remove deprecated warning
#define getcwd _getcwd
constexpr int MAXIMUM_PATH_LENGTH = _MAX_PATH;
#endif
#ifdef ECAL_OS_LINUX
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
constexpr int MAXIMUM_PATH_LENGTH = PATH_MAX;
#endif

#include "ecal_utils/filesystem.h"
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 0a47c89

Please sign in to comment.