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 <iostream>
#include <new>
namespace ELFIO {
@ -126,21 +127,17 @@ template <class T> class section_impl : public section
{
if ( get_type() != SHT_NOBITS ) {
delete[] data;
try {
data = new char[size];
}
catch ( const std::bad_alloc& ) {
data = 0;
data_size = 0;
size = 0;
}
data = new ( std::nothrow ) char[size];
if ( 0 != data && 0 != raw_data ) {
data_size = size;
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() );
}
else {
data_size = 2 * ( data_size + size );
char* new_data;
try {
new_data = new char[data_size];
}
catch ( const std::bad_alloc& ) {
new_data = 0;
size = 0;
}
data_size = 2 * ( data_size + size );
char* new_data = new ( std::nothrow ) char[data_size];
if ( 0 != new_data ) {
std::copy( data, data + get_size(), new_data );
std::copy( raw_data, raw_data + size,
@ -173,6 +164,9 @@ template <class T> class section_impl : public section
delete[] data;
data = new_data;
}
else {
size = 0;
}
}
set_size( get_size() + size );
}
@ -207,13 +201,7 @@ template <class T> class section_impl : public section
Elf_Xword size = get_size();
if ( 0 == data && SHT_NULL != get_type() && SHT_NOBITS != get_type() &&
size < get_stream_size() ) {
try {
data = new char[size + 1];
}
catch ( const std::bad_alloc& ) {
data = 0;
data_size = 0;
}
data = new ( std::nothrow ) char[size + 1];
if ( ( 0 != size ) && ( 0 != data ) ) {
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 = size;
}
else {
data_size = 0;
}
}
}

View File

@ -25,6 +25,7 @@ THE SOFTWARE.
#include <iostream>
#include <vector>
#include <new>
namespace ELFIO {
@ -167,12 +168,7 @@ template <class T> class segment_impl : public segment
data = 0;
}
else {
try {
data = new char[size + 1];
}
catch ( const std::bad_alloc& ) {
data = 0;
}
data = new (std::nothrow) char[size + 1];
if ( 0 != data ) {
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 )
{
try {
if ( argc != 2 ) {
std::cout << "Usage: anonymizer <file_name>\n";
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;
if ( argc != 2 ) {
std::cout << "Usage: anonymizer <file_name>\n";
return 1;
}
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;
}