summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/Unix
diff options
context:
space:
mode:
authorSam McCall <sam.mccall@gmail.com>2019-07-02 15:42:37 +0000
committerSam McCall <sam.mccall@gmail.com>2019-07-02 15:42:37 +0000
commitedf904efff6f188aa077ce3940d26dcef6f74ac6 (patch)
treefba5e34eb7335e5ca05cff7bae498f2179716fcb /llvm/lib/Support/Unix
parentc3d5bbee23395730fcbe557a18108b8713d41328 (diff)
downloadbcm5719-llvm-edf904efff6f188aa077ce3940d26dcef6f74ac6.tar.gz
bcm5719-llvm-edf904efff6f188aa077ce3940d26dcef6f74ac6.zip
getMainExecutable: handle realpath() failure, falling back to getprogpath().
Summary: Previously, we'd pass a nullptr to std::string and crash(). This case happens when the binary is deleted while being used (e.g. rebuilding clangd). Reviewers: kadircet Subscribers: ilya-biryukov, kristina, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D64068 llvm-svn: 364936
Diffstat (limited to 'llvm/lib/Support/Unix')
-rw-r--r--llvm/lib/Support/Unix/Path.inc20
1 files changed, 10 insertions, 10 deletions
diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index 761d183e91b..c64c0df19ad 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -222,20 +222,20 @@ std::string getMainExecutable(const char *argv0, void *MainAddr) {
// the program, and not the eventual binary file. Therefore, call realpath
// so this behaves the same on all platforms.
#if _POSIX_VERSION >= 200112 || defined(__GLIBC__)
- char *real_path = realpath(exe_path, NULL);
- std::string ret = std::string(real_path);
- free(real_path);
- return ret;
+ if (char *real_path = realpath(exe_path, NULL)) {
+ std::string ret = std::string(real_path);
+ free(real_path);
+ return ret;
+ }
#else
char real_path[MAXPATHLEN];
- realpath(exe_path, real_path);
- return std::string(real_path);
+ if (realpath(exe_path, real_path))
+ return std::string(real_path);
#endif
- } else {
- // Fall back to the classical detection.
- if (getprogpath(exe_path, argv0))
- return exe_path;
}
+ // Fall back to the classical detection.
+ if (getprogpath(exe_path, argv0))
+ return exe_path;
#elif defined(HAVE_DLFCN_H) && defined(HAVE_DLADDR)
// Use dladdr to get executable path if available.
Dl_info DLInfo;
OpenPOWER on IntegriCloud