[lld] [LLD] [COFF] Don't look up relative paths to parent directories in the search paths (PR #67858)

via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 29 14:37:38 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lld-coff

<details>
<summary>Changes</summary>

In b6c2f100c23bb715edbec57a4894f1ae551cd1d4, we allowed looking up relative paths within the search directories. This was done with the intention to allow resolving paths like <triple>/clang_rt.builtins.lib within a base search directory. However we shouldn't try to look up e.g. a path like ../../../../lib/libLLVMSupport.a in the search paths. In some cases, this could actually lead to accidental matches, for a file other than the one that was intended.

This should fix one aspect of #<!-- -->67779.

---
Full diff: https://github.com/llvm/llvm-project/pull/67858.diff


2 Files Affected:

- (modified) lld/COFF/Driver.cpp (+4-1) 
- (modified) lld/test/COFF/relative_search_paths.test (+4) 


``````````diff
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 61a04a74aa60278..c95ac626780a0cd 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -483,7 +483,10 @@ StringRef LinkerDriver::findFile(StringRef filename) {
     return filename;
   };
 
-  if (sys::path::is_absolute(filename))
+  bool hasParentDir = filename.starts_with("../") ||
+                      filename.starts_with("..\\") ||
+                      filename.contains("/../") || filename.contains("\\..\\");
+  if (sys::path::is_absolute(filename) || hasParentDir)
     return getFilename(filename);
   bool hasExt = filename.contains('.');
   for (StringRef dir : searchPaths) {
diff --git a/lld/test/COFF/relative_search_paths.test b/lld/test/COFF/relative_search_paths.test
index 8c89e9aa0c66827..488e4c2dfd5761b 100644
--- a/lld/test/COFF/relative_search_paths.test
+++ b/lld/test/COFF/relative_search_paths.test
@@ -2,3 +2,7 @@ We should be able to find libraries with relative search paths.
 # RUN: mkdir -p %t.dir/relative/path
 # RUN: cp %p/Inputs/std64.lib %t.dir/relative/path
 # RUN: lld-link %p/Inputs/hello64.obj /libpath:%t.dir relative/path/std64.lib /entry:main
+
+Check that we don't resolve file names like ../std64.lib relative to a libpath.
+# RUN: cp %p/Inputs/std64.lib %t.dir
+# RUN: not lld-link %p/Inputs/hello64.obj /libpath:%t.dir/relative ../std64.lib /entry:main

``````````

</details>


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


More information about the llvm-commits mailing list