Skip to content

Commit

Permalink
add robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
EmielBruijntjes committed Aug 24, 2023
1 parent 019c270 commit 2ba4237
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/programname.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <limits.h>
#if defined(_WIN32) || defined(_WIN64)
#include "Windows.h"
#define PATH_MAX MAX_PATH
#else
#include <unistd.h>
#endif
Expand Down Expand Up @@ -52,7 +53,14 @@ class ProgramName
ProgramName()
{
#if defined(_WIN32) || defined(_WIN64)
GetModuleFileNameA(NULL, _path, MAX_PATH);
// the the
auto size = GetModuleFileNameA(NULL, _path, PATH_MAX);

// -1 is returned on error, otherwise the size
_valid = size >= 0;

// set trailing null byte
_path[size == PATH_MAX ? PATH_MAX-1 : size] = '\0';
#else
// read the link target
auto size = readlink("/proc/self/exe", _path, PATH_MAX);
Expand Down

0 comments on commit 2ba4237

Please sign in to comment.