diff --git a/llvm/lib/Archive/ArchiveWriter.cpp b/llvm/lib/Archive/ArchiveWriter.cpp index 07f7d88dd821..04cc981f7960 100644 --- a/llvm/lib/Archive/ArchiveWriter.cpp +++ b/llvm/lib/Archive/ArchiveWriter.cpp @@ -348,7 +348,7 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress, { // Make sure they haven't opened up the file, not loaded it, // but are now trying to write it which would wipe out the file. - if (members.empty() && mapfile->size() > 8) { + if (members.empty() && mapfile && mapfile->size() > 8) { if (ErrMsg) *ErrMsg = "Can't write an archive not opened for writing"; return true; diff --git a/llvm/tools/llvm-ar/llvm-ar.cpp b/llvm/tools/llvm-ar/llvm-ar.cpp index a72a4c00190e..a6611a3e8091 100644 --- a/llvm/tools/llvm-ar/llvm-ar.cpp +++ b/llvm/tools/llvm-ar/llvm-ar.cpp @@ -717,6 +717,7 @@ int main(int argc, char **argv) { if (!Create) std::cerr << argv[0] << ": creating " << ArchivePath.toString() << "\n"; TheArchive = Archive::CreateEmpty(ArchivePath); + TheArchive->writeToDisk(); } else { std::string Error; TheArchive = Archive::OpenAndLoad(ArchivePath, &Error);