Remove usage of try/catch blocks

This commit is contained in:
Serge Lamikhov-Center 2020-08-28 22:38:40 -07:00
parent 4ea72e20b9
commit 937978973f
3 changed files with 39 additions and 61 deletions

View File

@ -25,6 +25,7 @@ THE SOFTWARE.
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <new>
namespace ELFIO { namespace ELFIO {
@ -126,21 +127,17 @@ template <class T> class section_impl : public section
{ {
if ( get_type() != SHT_NOBITS ) { if ( get_type() != SHT_NOBITS ) {
delete[] data; delete[] data;
try { data = new ( std::nothrow ) char[size];
data = new char[size];
}
catch ( const std::bad_alloc& ) {
data = 0;
data_size = 0;
size = 0;
}
if ( 0 != data && 0 != raw_data ) { if ( 0 != data && 0 != raw_data ) {
data_size = size; data_size = size;
std::copy( raw_data, raw_data + size, data ); std::copy( raw_data, raw_data + size, data );
} }
else {
data_size = 0;
}
} }
set_size( size ); set_size( data_size );
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -157,15 +154,9 @@ template <class T> class section_impl : public section
std::copy( raw_data, raw_data + size, data + get_size() ); std::copy( raw_data, raw_data + size, data + get_size() );
} }
else { else {
data_size = 2 * ( data_size + size ); data_size = 2 * ( data_size + size );
char* new_data; char* new_data = new ( std::nothrow ) char[data_size];
try {
new_data = new char[data_size];
}
catch ( const std::bad_alloc& ) {
new_data = 0;
size = 0;
}
if ( 0 != new_data ) { if ( 0 != new_data ) {
std::copy( data, data + get_size(), new_data ); std::copy( data, data + get_size(), new_data );
std::copy( raw_data, raw_data + size, std::copy( raw_data, raw_data + size,
@ -173,6 +164,9 @@ template <class T> class section_impl : public section
delete[] data; delete[] data;
data = new_data; data = new_data;
} }
else {
size = 0;
}
} }
set_size( get_size() + size ); set_size( get_size() + size );
} }
@ -207,13 +201,7 @@ template <class T> class section_impl : public section
Elf_Xword size = get_size(); Elf_Xword size = get_size();
if ( 0 == data && SHT_NULL != get_type() && SHT_NOBITS != get_type() && if ( 0 == data && SHT_NULL != get_type() && SHT_NOBITS != get_type() &&
size < get_stream_size() ) { size < get_stream_size() ) {
try { data = new ( std::nothrow ) char[size + 1];
data = new char[size + 1];
}
catch ( const std::bad_alloc& ) {
data = 0;
data_size = 0;
}
if ( ( 0 != size ) && ( 0 != data ) ) { if ( ( 0 != size ) && ( 0 != data ) ) {
stream.seekg( ( *convertor )( header.sh_offset ) ); stream.seekg( ( *convertor )( header.sh_offset ) );
@ -221,6 +209,9 @@ template <class T> class section_impl : public section
data[size] = 0; // Ensure data is ended with 0 to avoid oob read data[size] = 0; // Ensure data is ended with 0 to avoid oob read
data_size = size; data_size = size;
} }
else {
data_size = 0;
}
} }
} }

View File

@ -25,6 +25,7 @@ THE SOFTWARE.
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <new>
namespace ELFIO { namespace ELFIO {
@ -167,12 +168,7 @@ template <class T> class segment_impl : public segment
data = 0; data = 0;
} }
else { else {
try { data = new (std::nothrow) char[size + 1];
data = new char[size + 1];
}
catch ( const std::bad_alloc& ) {
data = 0;
}
if ( 0 != data ) { if ( 0 != data ) {
stream.read( data, size ); stream.read( data, size );

View File

@ -77,37 +77,28 @@ void process_string_table( const section* s, const std::string& filename )
int main( int argc, char** argv ) int main( int argc, char** argv )
{ {
try { if ( argc != 2 ) {
if ( argc != 2 ) { std::cout << "Usage: anonymizer <file_name>\n";
std::cout << "Usage: anonymizer <file_name>\n"; return 1;
return 1;
}
std::string filename = argv[1];
elfio reader;
if ( !reader.load( filename ) ) {
std::cerr << "File " << filename
<< " is not found or it is not an ELF file\n";
return 1;
}
for ( auto section = reader.sections.begin();
section != reader.sections.end(); ++section ) {
if ( ( *section )->get_type() == SHT_STRTAB &&
std::string( ( *section )->get_name() ) ==
std::string( ".strtab" ) ) {
process_string_table( *section, filename );
}
}
return 0;
} }
catch ( const std::string& s ) {
std::cerr << s << std::endl; std::string filename = argv[1];
elfio reader;
if ( !reader.load( filename ) ) {
std::cerr << "File " << filename
<< " is not found or it is not an ELF file\n";
return 1;
} }
catch ( const char* s ) {
std::cerr << s << std::endl; for ( auto section = reader.sections.begin();
section != reader.sections.end(); ++section ) {
if ( ( *section )->get_type() == SHT_STRTAB &&
std::string( ( *section )->get_name() ) ==
std::string( ".strtab" ) ) {
process_string_table( *section, filename );
}
} }
return 1; return 0;
} }