add robustness

This commit is contained in:
Emiel Bruijntjes 2023-08-24 21:46:39 +02:00
parent 019c270add
commit 2ba4237d57
1 changed files with 9 additions and 1 deletions

View File

@ -18,6 +18,7 @@
#include <limits.h> #include <limits.h>
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
#include "Windows.h" #include "Windows.h"
#define PATH_MAX MAX_PATH
#else #else
#include <unistd.h> #include <unistd.h>
#endif #endif
@ -52,7 +53,14 @@ public:
ProgramName() ProgramName()
{ {
#if defined(_WIN32) || defined(_WIN64) #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 #else
// read the link target // read the link target
auto size = readlink("/proc/self/exe", _path, PATH_MAX); auto size = readlink("/proc/self/exe", _path, PATH_MAX);