Use the FindProgramByName fallback only with known absolute paths.
authorDan Gohman <gohman@apple.com>
Thu, 2 Sep 2010 22:32:38 +0000 (22:32 +0000)
committerDan Gohman <gohman@apple.com>
Thu, 2 Sep 2010 22:32:38 +0000 (22:32 +0000)
I wasn't able to convince myself that all GetMainExecutable
implementations always return absolute paths; this prevents
unexpected behavior in case they ever don't.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@112888 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/SystemUtils.cpp

index 421b38111e0963600976e846d46352dd39f2c3af..c8b260c2e3dd9b4174fa224c9e124b0a11b2c5e7 100644 (file)
@@ -49,10 +49,16 @@ sys::Path llvm::FindExecutable(const std::string &ExeName,
     Result.appendComponent(ExeName);
     if (Result.canExecute())
       return Result;
-    // Expect to retrieve the pathname with suffix .exe.
-    Result = sys::Program::FindProgramByName(Result.str());
-    if (!Result.empty())
-      return Result;
+    // If the path is absolute (and it usually is), call FindProgramByName to
+    // allow it to try platform-specific logic, such as appending a .exe suffix
+    // on Windows. Don't do this if we somehow have a relative path, because
+    // we don't want to go searching the PATH and accidentally find an unrelated
+    // version of the program.
+    if (Result.isAbsolute()) {
+      Result = sys::Program::FindProgramByName(Result.str());
+      if (!Result.empty())
+        return Result;
+    }
   }
 
   return sys::Path();