[llvm] r364936 - getMainExecutable: handle realpath() failure, falling back to getprogpath().

Sam McCall via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 2 08:42:37 PDT 2019


Author: sammccall
Date: Tue Jul  2 08:42:37 2019
New Revision: 364936

URL: http://llvm.org/viewvc/llvm-project?rev=364936&view=rev
Log:
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

Modified:
    llvm/trunk/lib/Support/Unix/Path.inc

Modified: llvm/trunk/lib/Support/Unix/Path.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/Path.inc?rev=364936&r1=364935&r2=364936&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/Path.inc (original)
+++ llvm/trunk/lib/Support/Unix/Path.inc Tue Jul  2 08:42:37 2019
@@ -222,20 +222,20 @@ std::string getMainExecutable(const char
     // 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;




More information about the llvm-commits mailing list