[llvm] r315376 - Make the ELFFile constructor private.
Rafael Espindola via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 10 15:17:49 PDT 2017
Author: rafael
Date: Tue Oct 10 15:17:49 2017
New Revision: 315376
URL: http://llvm.org/viewvc/llvm-project?rev=315376&view=rev
Log:
Make the ELFFile constructor private.
With this all clients have to use the new create method which returns
an Expected.
Fixes a crash on invalid input.
Added:
llvm/trunk/test/Object/Inputs/invalid-buffer.elf
Modified:
llvm/trunk/include/llvm/Object/ELF.h
llvm/trunk/include/llvm/Object/ELFObjectFile.h
llvm/trunk/test/Object/invalid.test
Modified: llvm/trunk/include/llvm/Object/ELF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELF.h?rev=315376&r1=315375&r2=315376&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ELF.h (original)
+++ llvm/trunk/include/llvm/Object/ELF.h Tue Oct 10 15:17:49 2017
@@ -83,6 +83,8 @@ public:
private:
StringRef Buf;
+ ELFFile(StringRef Object);
+
public:
const Elf_Ehdr *getHeader() const {
return reinterpret_cast<const Elf_Ehdr *>(base());
@@ -112,7 +114,7 @@ public:
Expected<const Elf_Sym *> getRelocationSymbol(const Elf_Rel *Rel,
const Elf_Shdr *SymTab) const;
- ELFFile(StringRef Object);
+ static Expected<ELFFile> create(StringRef Object);
bool isMipsELF64() const {
return getHeader()->e_machine == ELF::EM_MIPS &&
@@ -345,9 +347,13 @@ ELFFile<ELFT>::getSectionStringTable(Elf
return getStringTable(&Sections[Index]);
}
+template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {}
+
template <class ELFT>
-ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {
- assert(sizeof(Elf_Ehdr) <= Buf.size() && "Invalid buffer");
+Expected<ELFFile<ELFT>> ELFFile<ELFT>::create(StringRef Object) {
+ if (sizeof(Elf_Ehdr) > Object.size())
+ return createError("Invalid buffer");
+ return ELFFile(Object);
}
template <class ELFT>
Modified: llvm/trunk/include/llvm/Object/ELFObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELFObjectFile.h?rev=315376&r1=315375&r2=315376&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ELFObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/ELFObjectFile.h Tue Oct 10 15:17:49 2017
@@ -211,8 +211,9 @@ public:
using Elf_Dyn = typename ELFFile<ELFT>::Elf_Dyn;
private:
- ELFObjectFile(MemoryBufferRef Object, const Elf_Shdr *DotDynSymSec,
- const Elf_Shdr *DotSymtabSec, ArrayRef<Elf_Word> ShndxTable);
+ ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
+ const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
+ ArrayRef<Elf_Word> ShndxTable);
protected:
ELFFile<ELFT> EF;
@@ -851,7 +852,10 @@ ELFObjectFile<ELFT>::getRela(DataRefImpl
template <class ELFT>
Expected<ELFObjectFile<ELFT>>
ELFObjectFile<ELFT>::create(MemoryBufferRef Object) {
- ELFFile<ELFT> EF(Object.getBuffer());
+ auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
+ if (Error E = EFOrErr.takeError())
+ return std::move(E);
+ auto EF = std::move(*EFOrErr);
auto SectionsOrErr = EF.sections();
if (!SectionsOrErr)
@@ -883,24 +887,25 @@ ELFObjectFile<ELFT>::create(MemoryBuffer
}
}
}
- return ELFObjectFile<ELFT>(Object, DotDynSymSec, DotSymtabSec, ShndxTable);
+ return ELFObjectFile<ELFT>(Object, EF, DotDynSymSec, DotSymtabSec,
+ ShndxTable);
}
template <class ELFT>
-ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object,
+ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
const Elf_Shdr *DotDynSymSec,
const Elf_Shdr *DotSymtabSec,
ArrayRef<Elf_Word> ShndxTable)
: ELFObjectFileBase(
getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
Object),
- EF(Data.getBuffer()), DotDynSymSec(DotDynSymSec),
- DotSymtabSec(DotSymtabSec), ShndxTable(ShndxTable) {}
+ EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
+ ShndxTable(ShndxTable) {}
template <class ELFT>
ELFObjectFile<ELFT>::ELFObjectFile(ELFObjectFile<ELFT> &&Other)
- : ELFObjectFile(Other.Data, Other.DotDynSymSec, Other.DotSymtabSec,
- Other.ShndxTable) {}
+ : ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec,
+ Other.DotSymtabSec, Other.ShndxTable) {}
template <class ELFT>
basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin() const {
Added: llvm/trunk/test/Object/Inputs/invalid-buffer.elf
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/Inputs/invalid-buffer.elf?rev=315376&view=auto
==============================================================================
--- llvm/trunk/test/Object/Inputs/invalid-buffer.elf (added)
+++ llvm/trunk/test/Object/Inputs/invalid-buffer.elf Tue Oct 10 15:17:49 2017
@@ -0,0 +1 @@
+ELF
\ No newline at end of file
Modified: llvm/trunk/test/Object/invalid.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/invalid.test?rev=315376&r1=315375&r2=315376&view=diff
==============================================================================
--- llvm/trunk/test/Object/invalid.test (original)
+++ llvm/trunk/test/Object/invalid.test Tue Oct 10 15:17:49 2017
@@ -81,3 +81,6 @@ INVALID-SECTION-NUM: section table goes
RUN: not llvm-readobj -r %p/Inputs/invalid-rel-sym.elf 2>&1 | FileCheck --check-prefix=INVALID-REL-SYM %s
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
More information about the llvm-commits
mailing list