[llvm-ar] Replace operation can silently convert a regular archive to a thin archive
In the following test-case:
$ echo "void foo(){}" > foo.c
$ gcc -c foo.c
$ llvm-ar qc q.a foo.o
$ llvm-ar rT q.a foo.o
The output should be:
error: Cannot convert existing library q.a to thin format
Instead, llvm-ar silently converts the existing archive to a thin archive.
This bug appears to exist in all current releases, including llvm14.
@llvm/issue-subscribers-tools-llvm-ar-llvm-ranlib
This appears to have been fixed on branch "main" as a side effect of: https://github.com/llvm/llvm-project/commit/1f71b5a38605a4f101af43288ae22feb89c8a469
We should still add a regression test for this issue.
Hmm... this is even more specific. Previous to https://github.com/llvm/llvm-project/commit/1f71b5a38605a4f101af43288ae22feb89c8a469 the behaviour is:
>llvm-ar.exe cr foo.a sub\a.txt
>llvm-ar.exe crT foo.a sub\a.txt
llvm-ar.exe: error: cannot convert a regular archive to a thin one
>del foo.a
>llvm-ar.exe cr foo.a a.txt
>llvm-ar.exe crT foo.a a.txt
>type foo.a
!<thin>
afterwards...
>llvm-ar.exe cr foo.a sub\a.txt
>llvm-ar.exe crT foo.a sub\a.txt
llvm-ar.exe: error: cannot convert a regular archive to a thin one
>del foo.a
>llvm-ar.exe cr foo.a a.txt
>llvm-ar.exe crT foo.a a.txt
llvm-ar.exe: error: cannot convert a regular archive to a thin one
So the file being replaced needs to be in the current directory!
@gbreynoo do you understand why having the file in the current directory would make a difference?
We should still add a regression test for this issue.
Regression test: https://reviews.llvm.org/D125785.
@bd1976bris thanks for looking into this issue. I have found the source of the problem:
After my change a check for the "regular to thin" error is made earlier in performWriteOperation and in both the cases above it raises an error. The following is in the context of llvm14 (and main before my change).
We work out if we need a replacement or insertion by comparing file paths before we get to the call of addChildMember where the "regular to thin" error can be raised. We do this by comparing the names of given input files and existing members, for regular archives we only compare the file names but for thin archives we always compare full paths. In the above case the comparison is strange as at this point we are comparing names in the manner we do with a thin archive due to the use of --thin but as it is actually a regular archive, the existing member only has a file name.
In the case of there being a directory in the given file path (sub\a.txt) the names do not match, it is considered an insertion and so the old member must be added to the new archive. addChildMember is called and the error is raised as expected.
In the case of no directory given (a.txt) the names match, it is considered a replace and the new member is added in place of the old one. addMember is called and this code path does not have the chance to raise the error.
The problem then was that the file comparison is reliant on if --thin is an input but we checked for the legality of the use of --thin vs the archive too late. My fix so we do the check early is the same fix I would do for this issue so I think we are fine now we have the test, I would consider this issue closed once that test is up.
@gbreynoo thanks for getting to the bottom of this. I'm closing...