Skip to content

Commit

Permalink
fix(Foundation): Path::self() throws SystemException when the path ca…
Browse files Browse the repository at this point in the history
…n't be acquired. #2282
  • Loading branch information
matejk committed Dec 5, 2023
1 parent f6bc0ab commit 9d65989
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 9 additions & 1 deletion Foundation/src/Path_UNIX.cpp
Expand Up @@ -49,6 +49,8 @@ std::string PathImpl::selfImpl()
std::uint32_t size = sizeof(buf);
if (_NSGetExecutablePath(buf, &size) == 0)
path = buf;
else
throw Poco::SystemException("Cannot get path of the current process.");
#elif POCO_OS == POCO_OS_FREE_BSD
int mib[4];
mib[0] = CTL_KERN;
Expand All @@ -58,6 +60,8 @@ std::string PathImpl::selfImpl()
std::size_t size = sizeof(buf);
if (sysctl(mib, 4, buf, &size, NULL, 0) == 0)
path = buf;
else
throw Poco::SystemException("Cannot get path of the current process.");
#elif POCO_OS == POCO_OS_NET_BSD
std::size_t size = sizeof(buf);
int n = readlink("/proc/curproc/exe", buf, size);
Expand All @@ -67,13 +71,17 @@ std::string PathImpl::selfImpl()
char * execName = getexecname();
if (execName)
path = execName;
else
throw Poco::SystemException("Cannot get path of the current process.");
#elif POCO_OS == POCO_OS_LINUX || POCO_OS == POCO_OS_ANDROID
const std::size_t size = sizeof(buf);
int n = readlink("/proc/self/exe", buf, size);
if (n > 0 && n < PATH_MAX)
path = buf;
else
throw Poco::SystemException("Cannot get path of the current process.");
#else
throw Poco::NotImplementedException("File path of the current program not implemented on this platform.");
throw Poco::NotImplementedException("File path of the current process not implemented on this platform.");
#endif

return path;
Expand Down
6 changes: 5 additions & 1 deletion Foundation/src/Path_WIN32U.cpp
Expand Up @@ -28,8 +28,12 @@ std::string PathImpl::selfImpl()
DWORD n = GetModuleFileNameW(NULL, buf.begin(), MAX_PATH_LEN);

if (n > 0 && n < MAX_PATH_LEN)
{
UnicodeConverter::toUTF8(buf.begin(), path);
return path;
return path;
}

throw SystemException("Cannot get path of the current process.");
}

std::string PathImpl::currentImpl()
Expand Down

0 comments on commit 9d65989

Please sign in to comment.