Skip to content

Commit 01aab49

Browse files
committed
don't assign an fs::path to a string
Seems to trip up MemorySanitizer on ARM32. Also applied various simplifications to the code using std::filesystem Signed-off-by: Rosen Penev <[email protected]>
1 parent 5ccb525 commit 01aab49

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

src/futils.cpp

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -336,24 +336,8 @@ Uri Uri::Parse(const std::string& uri) {
336336
}
337337

338338
std::string getProcessPath() {
339+
#if defined(__FreeBSD__)
339340
std::string ret("unknown");
340-
#if defined(_WIN32)
341-
HANDLE processHandle = nullptr;
342-
processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, GetCurrentProcessId());
343-
if (processHandle) {
344-
TCHAR filename[MAX_PATH];
345-
if (GetModuleFileNameEx(processHandle, nullptr, filename, MAX_PATH) != 0) {
346-
ret = filename;
347-
}
348-
CloseHandle(processHandle);
349-
}
350-
#elif __has_include(<libproc.h>)
351-
const int pid = getpid();
352-
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
353-
if (proc_pidpath(pid, pathbuf, sizeof(pathbuf)) > 0) {
354-
ret = pathbuf;
355-
}
356-
#elif defined(__FreeBSD__)
357341
unsigned int n;
358342
char buffer[PATH_MAX] = {};
359343
struct procstat* procstat = procstat_open_sysctl();
@@ -367,20 +351,28 @@ std::string getProcessPath() {
367351
procstat_freeprocs(procstat, procs);
368352
if (procstat)
369353
procstat_close(procstat);
370-
#elif defined(__sun__)
371-
// https://stackoverflow.com/questions/47472762/on-solaris-how-to-get-the-full-path-of-executable-of-running-process-programatic
372-
const char* proc = Internal::stringFormat("/proc/%d/path/a.out", getpid()).c_str();
373-
char path[500];
374-
ssize_t l = readlink(proc, path, sizeof(path) - 1);
375-
if (l > 0) {
376-
path[l] = 0;
377-
ret = path;
378-
}
379-
#elif defined(__unix__)
380-
ret = fs::read_symlink("/proc/self/exe");
381-
#endif
382354

383355
const size_t idxLastSeparator = ret.find_last_of(EXV_SEPARATOR_CHR);
384356
return ret.substr(0, idxLastSeparator);
357+
#else
358+
try {
359+
#if defined(_WIN32)
360+
TCHAR pathbuf[MAX_PATH];
361+
GetModuleFileName(nullptr, pathbuf, MAX_PATH);
362+
auto path = fs::path(pathbuf);
363+
#elif __has_include(<libproc.h>)
364+
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
365+
proc_pidpath(getpid(), pathbuf, sizeof(pathbuf));
366+
auto path = fs::path(pathbuf);
367+
#elif defined(__sun__)
368+
auto path = fs::read_symlink(Internal::stringFormat("/proc/%d/path/a.out", getpid()));
369+
#elif defined(__unix__)
370+
auto path = fs::read_symlink("/proc/self/exe");
371+
#endif
372+
return path.parent_path().string();
373+
} catch (const fs::filesystem_error&) {
374+
return "unknown";
375+
}
376+
#endif
385377
}
386378
} // namespace Exiv2

0 commit comments

Comments
 (0)