From 27158f9d90a529bd93c1c8f7790e3e8403ab0c18 Mon Sep 17 00:00:00 2001 From: Pavel Baykov Date: Thu, 2 Dec 2021 07:34:27 -0400 Subject: [PATCH] big fix warnings --- libraries/chain/block_database.cpp | 45 ++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/libraries/chain/block_database.cpp b/libraries/chain/block_database.cpp index b7a7441c..7675871e 100644 --- a/libraries/chain/block_database.cpp +++ b/libraries/chain/block_database.cpp @@ -103,8 +103,15 @@ void block_database::remove( const block_id_type& id ) index_entry e; auto index_pos = sizeof(e)*block_header::num_from_id(id); _block_num_to_pos.seekg( 0, _block_num_to_pos.end ); - if ( _block_num_to_pos.tellg() <= index_pos ) + + std::streampos s_pos = _block_num_to_pos.tellg(); + if (-1 == s_pos){ + FC_THROW_EXCEPTION(fc::key_not_found_exception, "Block ${id} not contained in block database, _block_num_to_pos.tellg failed", ("id", id)); + } + + if ( static_cast(s_pos) <= index_pos ){ FC_THROW_EXCEPTION(fc::key_not_found_exception, "Block ${id} not contained in block database", ("id", id)); + } _block_num_to_pos.seekg( index_pos ); _block_num_to_pos.read( (char*)&e, sizeof(e) ); @@ -118,20 +125,27 @@ void block_database::remove( const block_id_type& id ) } FC_CAPTURE_AND_RETHROW( (id) ) } bool block_database::contains( const block_id_type& id )const -{ +{ try { if( id == block_id_type() ) return false; index_entry e; auto index_pos = sizeof(e)*block_header::num_from_id(id); _block_num_to_pos.seekg( 0, _block_num_to_pos.end ); - if ( _block_num_to_pos.tellg() < index_pos + sizeof(e) ) + + std::streampos s_pos = _block_num_to_pos.tellg(); + if (-1 == s_pos){ + FC_THROW_EXCEPTION(fc::key_not_found_exception, "Block ${id} not contained in block database, _block_num_to_pos.tellg failed", ("id", id)); + } + + if ( static_cast(s_pos) < index_pos + sizeof(e) ) return false; + _block_num_to_pos.seekg( index_pos ); _block_num_to_pos.read( (char*)&e, sizeof(e) ); return e.block_id == id && e.block_size > 0; -} +} FC_CAPTURE_AND_RETHROW( (id) ) } block_id_type block_database::fetch_block_id( uint32_t block_num )const { @@ -156,7 +170,13 @@ optional block_database::fetch_optional( const block_id_type& id ) index_entry e; auto index_pos = sizeof(e)*block_header::num_from_id(id); _block_num_to_pos.seekg( 0, _block_num_to_pos.end ); - if ( _block_num_to_pos.tellg() <= index_pos ) + std::streampos s_pos = _block_num_to_pos.tellg(); + + if (-1 == s_pos){ + FC_THROW_EXCEPTION(fc::key_not_found_exception, "Block ${id} not contained in block database, _block_num_to_pos.tellg failed", ("id", id)); + } + + if ( static_cast(s_pos) <= index_pos ) return {}; _block_num_to_pos.seekg( index_pos ); @@ -188,7 +208,12 @@ optional block_database::fetch_by_number( uint32_t block_num )cons index_entry e; auto index_pos = sizeof(e)*block_num; _block_num_to_pos.seekg( 0, _block_num_to_pos.end ); - if ( _block_num_to_pos.tellg() <= index_pos ) + std::streampos s_pos = _block_num_to_pos.tellg(); + if (-1 == s_pos){ + FC_THROW_EXCEPTION(fc::key_not_found_exception, "Block ${block_num} not contained in block database, _block_num_to_pos.tellg failed", ("block_num", block_num)); + } + + if ( static_cast(s_pos) <= index_pos ) return {}; _block_num_to_pos.seekg( index_pos, _block_num_to_pos.beg ); @@ -217,7 +242,11 @@ optional block_database::last_index_entry()const { _block_num_to_pos.seekg( 0, _block_num_to_pos.end ); std::streampos pos = _block_num_to_pos.tellg(); - if( pos < sizeof(index_entry) ) + if (-1 == pos){ + FC_THROW_EXCEPTION(fc::key_not_found_exception, "last_index_entry tellg failed"); + } + + if( static_cast(pos) < sizeof(index_entry) ) return optional(); pos -= pos % sizeof(index_entry); @@ -230,7 +259,7 @@ optional block_database::last_index_entry()const { _block_num_to_pos.seekg( pos ); _block_num_to_pos.read( (char*)&e, sizeof(e) ); if( _block_num_to_pos.gcount() == sizeof(e) && e.block_size > 0 - && e.block_pos + e.block_size <= blocks_size ) + && e.block_pos + static_cast(e.block_size) <= static_cast(blocks_size) ) try { vector data( e.block_size );