From 2ba4237d5721f699ebace5b160c938402737ed96 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Thu, 24 Aug 2023 21:46:39 +0200 Subject: [PATCH] add robustness --- src/programname.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/programname.h b/src/programname.h index 5cb97aaa..c66f71c8 100644 --- a/src/programname.h +++ b/src/programname.h @@ -18,6 +18,7 @@ #include #if defined(_WIN32) || defined(_WIN64) #include "Windows.h" +#define PATH_MAX MAX_PATH #else #include #endif @@ -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);