diff options
author | Martin Storsjo <martin@martin.st> | 2019-08-29 08:59:56 +0000 |
---|---|---|
committer | Martin Storsjo <martin@martin.st> | 2019-08-29 08:59:56 +0000 |
commit | edb6ab9ba66c8fa751e47a4fc87f323f614070de (patch) | |
tree | 45469d34e66c6e245427cc18967707b976f8713c /llvm/lib/Object/COFFObjectFile.cpp | |
parent | 357a40ec7c20c075c985079c8591faea1ed79c0d (diff) | |
download | bcm5719-llvm-edb6ab9ba66c8fa751e47a4fc87f323f614070de.tar.gz bcm5719-llvm-edb6ab9ba66c8fa751e47a4fc87f323f614070de.zip |
[COFF] Add a bounds checking helper for iterating a coff_resource_dir_table
Instead of blindly incrementing pointers in llvm-readobj, use this
helper, which does bounds checking against the available section
data.
Differential Revision: https://reviews.llvm.org/D66818
llvm-svn: 370310
Diffstat (limited to 'llvm/lib/Object/COFFObjectFile.cpp')
-rw-r--r-- | llvm/lib/Object/COFFObjectFile.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/llvm/lib/Object/COFFObjectFile.cpp b/llvm/lib/Object/COFFObjectFile.cpp index b3dbf75a5c3..368a6d10344 100644 --- a/llvm/lib/Object/COFFObjectFile.cpp +++ b/llvm/lib/Object/COFFObjectFile.cpp @@ -1696,6 +1696,17 @@ ResourceSectionRef::getTableAtOffset(uint32_t Offset) { return *Table; } +Expected<const coff_resource_dir_entry &> +ResourceSectionRef::getTableEntryAtOffset(uint32_t Offset) { + const coff_resource_dir_entry *Entry = nullptr; + + BinaryStreamReader Reader(BBS); + Reader.setOffset(Offset); + RETURN_IF_ERROR(Reader.readObject(Entry)); + assert(Entry != nullptr); + return *Entry; +} + Expected<const coff_resource_dir_table &> ResourceSectionRef::getEntrySubDir(const coff_resource_dir_entry &Entry) { return getTableAtOffset(Entry.Offset.value()); @@ -1704,3 +1715,14 @@ ResourceSectionRef::getEntrySubDir(const coff_resource_dir_entry &Entry) { Expected<const coff_resource_dir_table &> ResourceSectionRef::getBaseTable() { return getTableAtOffset(0); } + +Expected<const coff_resource_dir_entry &> +ResourceSectionRef::getTableEntry(const coff_resource_dir_table &Table, + uint32_t Index) { + if (Index >= (uint32_t)(Table.NumberOfNameEntries + Table.NumberOfIDEntries)) + return createStringError(object_error::parse_failed, "index out of range"); + const uint8_t *TablePtr = reinterpret_cast<const uint8_t *>(&Table); + ptrdiff_t TableOffset = TablePtr - BBS.data().data(); + return getTableEntryAtOffset(TableOffset + sizeof(Table) + + Index * sizeof(coff_resource_dir_entry)); +} |