Skip to content

Commit

Permalink
Fix warnings and memory error in ApplicationPaths
Browse files Browse the repository at this point in the history
  • Loading branch information
multiplemonomials committed Oct 14, 2023
1 parent 8ac808a commit cdba583
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/ApplicationPaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include <limits.h>
#include <libgen.h>
#include <unistd.h>
#include <stdexcept>
#include <cstring>

#if defined(__sun)
#define PROC_SELF_EXE "/proc/self/path/a.out"
Expand Down Expand Up @@ -76,19 +78,12 @@ std::string getExecutableDir() {

std::string getExecutablePath() {
char rawPathName[PATH_MAX];
(void)realpath(PROC_SELF_EXE, rawPathName);
if(realpath(PROC_SELF_EXE, rawPathName) == nullptr)
{
throw std::runtime_error("realpath() failed!");
}
return std::string(rawPathName);
}

std::string getExecutableDir() {
std::string executablePath = getExecutablePath();
char *executablePathStr = new char[executablePath.length() + 1];
strcpy(executablePathStr, executablePath.c_str());
char* executableDir = dirname(executablePathStr);
delete [] executablePathStr;
return std::string(executableDir);
}

#endif

#ifdef __APPLE__
Expand All @@ -102,14 +97,24 @@ std::string getExecutablePath() {
}
return std::string(realPathName);
}
#endif

#if defined(__APPLE__) || defined(__linux__)
std::string getExecutableDir() {
std::string executablePath = getExecutablePath();

// Create a new char* in memory containing the executable path
char *executablePathStr = new char[executablePath.length() + 1];
strcpy(executablePathStr, executablePath.c_str());
char* executableDir = dirname(executablePathStr);

// Run dirname on executablePathStr (dirname modifies the memory in place)
char* executableDirStr = dirname(executablePathStr);

// Copy the path into a new string, then delete the mem we allocated
std::string executableDir(executableDirStr);
delete [] executablePathStr;
return std::string(executableDir);

return executableDir;
}
#endif

Expand Down

0 comments on commit cdba583

Please sign in to comment.