[PATCH] D44225: Fix identification of COFF executable files
Rafael Avila de Espindola via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 7 16:24:07 PST 2018
LGTM.
A followup deleting this completely like Reid Kleckner suggests also
LGTM.
Cheers,
Rafael
Zachary Turner via Phabricator via llvm-commits
<llvm-commits at lists.llvm.org> writes:
> zturner updated this revision to Diff 137482.
> zturner added a comment.
>
> Always read the whole file.
>
>
> https://reviews.llvm.org/D44225
>
> Files:
> llvm/lib/BinaryFormat/Magic.cpp
>
>
> Index: llvm/lib/BinaryFormat/Magic.cpp
> ===================================================================
> --- llvm/lib/BinaryFormat/Magic.cpp
> +++ 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 @@
> }
>
> 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();
> }
>
>
> Index: llvm/lib/BinaryFormat/Magic.cpp
> ===================================================================
> --- llvm/lib/BinaryFormat/Magic.cpp
> +++ 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 @@
> }
>
> 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();
> }
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list