Skip to content

Commit

Permalink
Changed cwdPath logic to not leak memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peguen committed Nov 5, 2024
1 parent ecb5179 commit fa0a622
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 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,22 @@
#include "configuration_to_yaml.h"
#endif


// for cwd
#ifdef ECAL_OS_WINDOWS
#include <limits>
#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>
#include <limits.h>
constexpr int MAXIMUM_PATH_LENGTH = PATH_MAX;
#endif

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

0 comments on commit fa0a622

Please sign in to comment.