[llvm] [Windows] Avoid loading shared system libraries from user directory (PR #90520)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 30 05:12:51 PDT 2024


================
@@ -44,8 +44,24 @@ InitLLVM::InitLLVM(int &Argc, const char **&Argv,
   Initialized = true;
 #endif
 #ifdef _WIN32
-  // Avoid searching the directory from which the application is loaded.
-  SetDllDirectoryA("");
+  // Avoid searching user directories for shared libraries:
+  //   Avoid searching the current directory:
+  (void)SetDllDirectoryA("");
+  wchar_t CurrentPath[MAX_PATH];
+  (void)GetCurrentDirectory(MAX_PATH, CurrentPath);
+  //   Avoid searching the directory from which the application is loaded:
+  wchar_t Appname[MAX_PATH];
+  (void)GetModuleFileName(NULL, Appname, MAX_PATH);
+  std::array<wchar_t, MAX_PATH> AN;
+  std::copy(std::begin(Appname), std::end(Appname), std::begin(AN));
+  std::string LP{AN.begin(), AN.end()};
+  LP = LP.substr(0, LP.rfind('\\'));
+  auto Path = std::wstring(LP.begin(), LP.end());
+  auto LoadPath = Path.c_str();
+  (void)SetCurrentDirectory(LoadPath);
+  (void)SetDllDirectoryA("");
+  (void)SetCurrentDirectory(CurrentPath); // reset cwd
+  // TODO: check for errors
----------------
arsenm wrote:

Always check for errors, this can't be left as todo 

https://github.com/llvm/llvm-project/pull/90520


More information about the llvm-commits mailing list