[PATCH] D25233: [ELF] - Do not crash when unable to parse ELF object file.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 4 05:30:34 PDT 2016


grimar created this revision.
grimar added reviewers: ruiu, rafael, davide.
grimar added subscribers: llvm-commits, grimar, evgeny777.

createELFObj() may call error(...), for example when file is too short.
In that case header is not set and following line lead to crash:

EMachine = ELFObj.getHeader()->e_machine;

Patch fixes the issue.


https://reviews.llvm.org/D25233

Files:
  ELF/InputFiles.cpp
  test/ELF/invalid/Inputs/too-short.elf
  test/ELF/invalid/too-short.s


Index: test/ELF/invalid/too-short.s
===================================================================
--- test/ELF/invalid/too-short.s
+++ test/ELF/invalid/too-short.s
@@ -0,0 +1,5 @@
+# REQUIRES: x86
+
+## too-short.elf file is a truncated ELF.
+# RUN: not ld.lld %S/Inputs/too-short.elf -o %t 2>&1 | FileCheck %s
+# CHECK: failed to read
Index: ELF/InputFiles.cpp
===================================================================
--- ELF/InputFiles.cpp
+++ ELF/InputFiles.cpp
@@ -70,6 +70,8 @@
 template <class ELFT>
 ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB)
     : InputFile(K, MB), ELFObj(createELFObj<ELFT>(MB)) {
+  if (HasError)
+    return;
   EKind = getELFKind<ELFT>();
   EMachine = ELFObj.getHeader()->e_machine;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25233.73464.patch
Type: text/x-patch
Size: 763 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161004/caf7b8b9/attachment.bin>


More information about the llvm-commits mailing list