diff options
author | Zachary Turner <zturner@google.com> | 2018-03-08 19:45:20 +0000 |
---|---|---|
committer | Zachary Turner <zturner@google.com> | 2018-03-08 19:45:20 +0000 |
commit | 9899b5feb69607ff56a14c1eccdc08b8bf1f23d2 (patch) | |
tree | 87e9fe859d7b28ae9406ce65a14a8b57603b1f00 /llvm/lib | |
parent | b575f46b6d2a1aeb7fdbbebd2654fa24165afae9 (diff) | |
download | bcm5719-llvm-9899b5feb69607ff56a14c1eccdc08b8bf1f23d2.tar.gz bcm5719-llvm-9899b5feb69607ff56a14c1eccdc08b8bf1f23d2.zip |
Fix detection of COFF executable files.
One overload of this function would try to identify a file
by opening it and using the first 32 bytes to identify the magic
of the file. This didn't work properly when more than 32 bytes
is actually needed for magic detection to succeed. So now we
have this overload read in the entire file.
Differential Revision: https://reviews.llvm.org/D44225
llvm-svn: 327050
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/BinaryFormat/Magic.cpp | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/llvm/lib/BinaryFormat/Magic.cpp b/llvm/lib/BinaryFormat/Magic.cpp index 22ea2280433..afc217c6a51 100644 --- a/llvm/lib/BinaryFormat/Magic.cpp +++ b/llvm/lib/BinaryFormat/Magic.cpp @@ -14,6 +14,7 @@ #include "llvm/BinaryFormat/MachO.h" #include "llvm/Support/Endian.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/MemoryBuffer.h" #if !defined(_MSC_VER) && !defined(__MINGW32__) #include <unistd.h> @@ -205,15 +206,12 @@ file_magic llvm::identify_magic(StringRef Magic) { } std::error_code llvm::identify_magic(const Twine &Path, file_magic &Result) { - int FD; - if (std::error_code EC = openFileForRead(Path, FD)) - return EC; + auto FileOrError = MemoryBuffer::getFile(Path); + if (!FileOrError) + return FileOrError.getError(); - char Buffer[32]; - int Length = read(FD, Buffer, sizeof(Buffer)); - if (close(FD) != 0 || Length < 0) - return std::error_code(errno, std::generic_category()); + std::unique_ptr<MemoryBuffer> FileBuffer = std::move(*FileOrError); + Result = identify_magic(FileBuffer->getBuffer()); - Result = identify_magic(StringRef(Buffer, Length)); return std::error_code(); } |