Prevents seg fault described in #42

This commit is contained in:
Serge Lamikhov-Center 2020-06-10 06:53:29 -07:00
parent a2ee85d245
commit 9cf8821c03
4 changed files with 47 additions and 3 deletions

2
.vscode/launch.json vendored
View File

@ -30,7 +30,7 @@
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/examples/elfdump/elfdump",
"args": ["test"],
"args": ["elf_examples/crash.elf"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],

View File

@ -552,3 +552,36 @@ BOOST_AUTO_TEST_CASE( null_section_inside_segment )
BOOST_CHECK_EQUAL( elf.load(f1), true );
BOOST_CHECK_EQUAL( elf.save(f2), true );
}
////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE(invalid_file)
{
elfio elf;
std::string name;
ELFIO::Elf64_Addr value;
ELFIO::Elf_Xword size;
unsigned char bind;
unsigned char type;
ELFIO::Elf_Half section_index;
unsigned char other;
std::string in = "../elf_examples/crash.elf";
BOOST_REQUIRE_EQUAL(elf.load(in), true);
section *psymsec = elf.sections[".symtab"];
BOOST_REQUIRE_NE(psymsec, (void*)0);
const symbol_section_accessor symbols(elf, psymsec);
BOOST_CHECK_EQUAL( true,
symbols.get_symbol( "main", value, size, bind,
type, section_index, other ) );
BOOST_CHECK_EQUAL( 0x402560, value );
BOOST_CHECK_EQUAL(
true, symbols.get_symbol( "frame_dummy", value, size, bind,
type, section_index, other ) );
BOOST_CHECK_EQUAL( 0x402550, value );
BOOST_CHECK_EQUAL( false,
symbols.get_symbol( 0x00400498, name, size, bind,
type, section_index, other ) );
}

BIN
elf_examples/crash.elf Normal file

Binary file not shown.

View File

@ -105,6 +105,17 @@ class symbol_section_accessor_template
ret = true;
}
}
else {
for ( Elf_Xword i = 0; i < get_symbols_num() && !ret; i++ ) {
std::string symbol_name;
if ( get_symbol( i, symbol_name, value, size, bind, type,
section_index, other) ) {
if ( symbol_name == name ) {
ret = true;
}
}
}
}
return ret;
}
@ -240,7 +251,7 @@ class symbol_section_accessor_template
template< class T >
const T*
generic_get_symbol_ptr(Elf_Xword index) const {
if ( index < get_symbols_num() ) {
if ( 0 != symbol_section->get_data() && index < get_symbols_num() ) {
const T* pSym = reinterpret_cast<const T*>(
symbol_section->get_data() +
index * symbol_section->get_entry_size() );
@ -282,7 +293,7 @@ class symbol_section_accessor_template
{
bool ret = false;
if ( index < get_symbols_num() ) {
if ( 0 != symbol_section->get_data() && index < get_symbols_num() ) {
const T* pSym = reinterpret_cast<const T*>(
symbol_section->get_data() +
index * symbol_section->get_entry_size() );