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

It manifests as an incorrect name in --print-archive-stats=.
This commit is contained in:
Fangrui Song 2022-07-26 14:36:56 -07:00
parent e27a029ed0
commit 6d0b42745e
2 changed files with 4 additions and 4 deletions

View File

@ -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});
}

View File

@ -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 /: {{.*}}