[lld] 6d0b427 - [ELF] addLibrary: fix a use-after-free bug in archiveName

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 26 14:37:00 PDT 2022


Author: Fangrui Song
Date: 2022-07-26T14:36:57-07:00
New Revision: 6d0b42745e55c703b779740f5316adbac8ef2f25

URL: https://github.com/llvm/llvm-project/commit/6d0b42745e55c703b779740f5316adbac8ef2f25
DIFF: https://github.com/llvm/llvm-project/commit/6d0b42745e55c703b779740f5316adbac8ef2f25.diff

LOG: [ELF] addLibrary: fix a use-after-free bug in archiveName

It manifests as an incorrect name in --print-archive-stats=.

Added: 
    

Modified: 
    lld/ELF/Driver.cpp
    lld/test/ELF/print-archive-stats.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 36d4e3b50fa54..6c0fd3139e877 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -290,7 +290,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
 // Add a given library by searching it from input search paths.
 void LinkerDriver::addLibrary(StringRef name) {
   if (Optional<std::string> path = searchLibrary(name))
-    addFile(*path, /*withLOption=*/true);
+    addFile(saver().save(*path), /*withLOption=*/true);
   else
     error("unable to find library -l" + name, ErrorTag::LibNotFound, {name});
 }

diff  --git a/lld/test/ELF/print-archive-stats.s b/lld/test/ELF/print-archive-stats.s
index eb1ce5e4768ba..61e571e352a57 100644
--- a/lld/test/ELF/print-archive-stats.s
+++ b/lld/test/ELF/print-archive-stats.s
@@ -21,12 +21,12 @@
 # RUN: ld.lld a.o %t/weak.a 1.a --print-archive-stats=- -o /dev/null | 
diff  a.txt -
 
 ## The second 1.a has 0 fetched member.
-# RUN: ld.lld a.o %t/weak.a 1.a 1.a --print-archive-stats=- -o /dev/null | \
+# RUN: ld.lld a.o %t/weak.a -L. -l:1.a -l:1.a --print-archive-stats=- -o /dev/null | \
 # RUN:   FileCheck --check-prefix=CHECK2 %s
 # CHECK2:      members	extracted	archive
 # CHECK2-NEXT: 1	0	{{.*}}weak.a
-# CHECK2-NEXT: 3	2	1.a
-# CHECK2-NEXT: 3	0	1.a
+# CHECK2-NEXT: 3	2	{{.*}}1.a
+# CHECK2-NEXT: 3	0	{{.*}}1.a
 
 # RUN: not ld.lld -shared a.o --print-archive-stats=/ -o /dev/null 2>&1 | FileCheck --check-prefix=ERR %s
 # ERR: error: --print-archive-stats=: cannot open /: {{.*}}


        


More information about the llvm-commits mailing list