Fix buffer overflow.

We were reading past the end of the buffer.

llvm-svn: 316143
This commit is contained in:
Rafael Espindola 2017-10-19 01:25:48 +00:00
parent 2a0a8fb6bc
commit 55680d0add
3 changed files with 4 additions and 1 deletions

View File

@ -185,7 +185,7 @@ file_magic llvm::identify_magic(StringRef Magic) {
if (startswith(Magic, "MZ") && Magic.size() >= 0x3c + 4) {
uint32_t off = read32le(Magic.data() + 0x3c);
// PE/COFF file, either EXE or DLL.
if (off < Magic.size() &&
if (off + sizeof(COFF::PEMagic) <= Magic.size() &&
memcmp(Magic.data() + off, COFF::PEMagic, sizeof(COFF::PEMagic)) == 0)
return file_magic::pecoff_executable;
}

Binary file not shown.

View File

@ -86,3 +86,6 @@ INVALID-REL-SYM: invalid section offset
RUN: not llvm-readobj -r %p/Inputs/invalid-buffer.elf 2>&1 | FileCheck --check-prefix=INVALID-BUFFER %s
INVALID-BUFFER: Invalid buffer
RUN: not llvm-readobj %p/Inputs/invalid-coff-header-too-small 2>&1 | FileCheck --check-prefix=COFF-HEADER %s
COFF-HEADER: The file was not recognized as a valid object file