From 7c0abb1785df8e5954b3b833c1d4c2c8d020dd63 Mon Sep 17 00:00:00 2001 From: Serge Lamikhov-Center Date: Sat, 12 Nov 2022 19:03:39 +0200 Subject: [PATCH] 'noexcept' is added to section and segment API --- elfio/elfio_section.hpp | 57 ++++++++++++++++++++++++----------------- elfio/elfio_segment.hpp | 49 +++++++++++++++++++---------------- 2 files changed, 61 insertions(+), 45 deletions(-) diff --git a/elfio/elfio_section.hpp b/elfio/elfio_section.hpp index 62064a7..c0fdd31 100644 --- a/elfio/elfio_section.hpp +++ b/elfio/elfio_section.hpp @@ -50,23 +50,25 @@ class section ELFIO_GET_SET_ACCESS_DECL( Elf_Word, name_string_offset ); ELFIO_GET_ACCESS_DECL( Elf64_Off, offset ); - virtual const char* get_data() const = 0; - virtual void set_data( const char* raw_data, Elf_Word size ) = 0; - virtual void set_data( const std::string& data ) = 0; - virtual void append_data( const char* raw_data, Elf_Word size ) = 0; - virtual void append_data( const std::string& data ) = 0; - virtual size_t get_stream_size() const = 0; - virtual void set_stream_size( size_t value ) = 0; + virtual const char* get_data() const noexcept = 0; + virtual void set_data( const char* raw_data, Elf_Word size ) noexcept = 0; + virtual void set_data( const std::string& data ) noexcept = 0; + virtual void append_data( const char* raw_data, + Elf_Word size ) noexcept = 0; + virtual void append_data( const std::string& data ) noexcept = 0; + virtual size_t get_stream_size() const noexcept = 0; + virtual void set_stream_size( size_t value ) noexcept = 0; protected: ELFIO_SET_ACCESS_DECL( Elf64_Off, offset ); ELFIO_SET_ACCESS_DECL( Elf_Half, index ); - virtual bool load( std::istream& stream, std::streampos header_offset ) = 0; + virtual bool load( std::istream& stream, + std::streampos header_offset ) noexcept = 0; virtual void save( std::ostream& stream, std::streampos header_offset, - std::streampos data_offset ) = 0; - virtual bool is_address_initialized() const = 0; + std::streampos data_offset ) noexcept = 0; + virtual bool is_address_initialized() const noexcept = 0; }; template class section_impl : public section @@ -114,13 +116,16 @@ template class section_impl : public section } //------------------------------------------------------------------------------ - bool is_address_initialized() const override { return is_address_set; } + bool is_address_initialized() const noexcept override + { + return is_address_set; + } //------------------------------------------------------------------------------ - const char* get_data() const override { return data.get(); } + const char* get_data() const noexcept override { return data.get(); } //------------------------------------------------------------------------------ - void set_data( const char* raw_data, Elf_Word size ) override + void set_data( const char* raw_data, Elf_Word size ) noexcept override { if ( get_type() != SHT_NOBITS ) { data = std::unique_ptr( new ( std::nothrow ) char[size] ); @@ -140,13 +145,13 @@ template class section_impl : public section } //------------------------------------------------------------------------------ - void set_data( const std::string& str_data ) override + void set_data( const std::string& str_data ) noexcept override { return set_data( str_data.c_str(), (Elf_Word)str_data.size() ); } //------------------------------------------------------------------------------ - void append_data( const char* raw_data, Elf_Word size ) override + void append_data( const char* raw_data, Elf_Word size ) noexcept override { if ( get_type() != SHT_NOBITS ) { if ( get_size() + size < data_size ) { @@ -176,16 +181,19 @@ template class section_impl : public section } //------------------------------------------------------------------------------ - void append_data( const std::string& str_data ) override + void append_data( const std::string& str_data ) noexcept override { return append_data( str_data.c_str(), (Elf_Word)str_data.size() ); } //------------------------------------------------------------------------------ - size_t get_stream_size() const override { return stream_size; } + size_t get_stream_size() const noexcept override { return stream_size; } //------------------------------------------------------------------------------ - void set_stream_size( size_t value ) override { stream_size = value; } + void set_stream_size( size_t value ) noexcept override + { + stream_size = value; + } //------------------------------------------------------------------------------ protected: @@ -196,7 +204,8 @@ template class section_impl : public section void set_index( const Elf_Half& value ) noexcept override { index = value; } //------------------------------------------------------------------------------ - bool load( std::istream& stream, std::streampos header_offset ) override + bool load( std::istream& stream, + std::streampos header_offset ) noexcept override { header = { 0 }; @@ -255,7 +264,7 @@ template class section_impl : public section //------------------------------------------------------------------------------ void save( std::ostream& stream, std::streampos header_offset, - std::streampos data_offset ) override + std::streampos data_offset ) noexcept override { if ( 0 != get_index() ) { header.sh_offset = decltype( header.sh_offset )( data_offset ); @@ -272,7 +281,8 @@ template class section_impl : public section //------------------------------------------------------------------------------ private: //------------------------------------------------------------------------------ - void save_header( std::ostream& stream, std::streampos header_offset ) const + void save_header( std::ostream& stream, + std::streampos header_offset ) const noexcept { adjust_stream_size( stream, header_offset ); stream.write( reinterpret_cast( &header ), @@ -280,7 +290,8 @@ template class section_impl : public section } //------------------------------------------------------------------------------ - void save_data( std::ostream& stream, std::streampos data_offset ) const + void save_data( std::ostream& stream, + std::streampos data_offset ) const noexcept { adjust_stream_size( stream, data_offset ); @@ -300,7 +311,7 @@ template class section_impl : public section //------------------------------------------------------------------------------ private: - T header = { 0 }; + T header = {}; Elf_Half index = 0; std::string name; std::unique_ptr data; diff --git a/elfio/elfio_segment.hpp b/elfio/elfio_segment.hpp index b4fb872..1ae3c5b 100644 --- a/elfio/elfio_segment.hpp +++ b/elfio/elfio_segment.hpp @@ -47,27 +47,28 @@ class segment ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, memory_size ); ELFIO_GET_ACCESS_DECL( Elf64_Off, offset ); - virtual const char* get_data() const = 0; + virtual const char* get_data() const noexcept = 0; - virtual Elf_Half add_section( section* psec, Elf_Xword addr_align ) = 0; + virtual Elf_Half add_section( section* psec, + Elf_Xword addr_align ) noexcept = 0; virtual Elf_Half add_section_index( Elf_Half index, - Elf_Xword addr_align ) = 0; - virtual Elf_Half get_sections_num() const = 0; - virtual Elf_Half get_section_index_at( Elf_Half num ) const = 0; - virtual bool is_offset_initialized() const = 0; + Elf_Xword addr_align ) noexcept = 0; + virtual Elf_Half get_sections_num() const noexcept = 0; + virtual Elf_Half get_section_index_at( Elf_Half num ) const noexcept = 0; + virtual bool is_offset_initialized() const noexcept = 0; protected: ELFIO_SET_ACCESS_DECL( Elf64_Off, offset ); ELFIO_SET_ACCESS_DECL( Elf_Half, index ); - virtual const std::vector& get_sections() const = 0; + virtual const std::vector& get_sections() const noexcept = 0; virtual bool load( std::istream& stream, std::streampos header_offset, - bool is_lazy ) = 0; + bool is_lazy ) noexcept = 0; virtual void save( std::ostream& stream, std::streampos header_offset, - std::streampos data_offset ) = 0; + std::streampos data_offset ) noexcept = 0; }; //------------------------------------------------------------------------------ @@ -96,7 +97,7 @@ template class segment_impl : public segment Elf_Half get_index() const noexcept override { return index; } //------------------------------------------------------------------------------ - const char* get_data() const override + const char* get_data() const noexcept override { if ( is_lazy ) { load_data(); @@ -107,7 +108,7 @@ template class segment_impl : public segment //------------------------------------------------------------------------------ Elf_Half add_section_index( Elf_Half sec_index, - Elf_Xword addr_align ) override + Elf_Xword addr_align ) noexcept override { sections.emplace_back( sec_index ); if ( addr_align > get_align() ) { @@ -118,19 +119,20 @@ template class segment_impl : public segment } //------------------------------------------------------------------------------ - Elf_Half add_section( section* psec, Elf_Xword addr_align ) override + Elf_Half add_section( section* psec, + Elf_Xword addr_align ) noexcept override { return add_section_index( psec->get_index(), addr_align ); } //------------------------------------------------------------------------------ - Elf_Half get_sections_num() const override + Elf_Half get_sections_num() const noexcept override { return (Elf_Half)sections.size(); } //------------------------------------------------------------------------------ - Elf_Half get_section_index_at( Elf_Half num ) const override + Elf_Half get_section_index_at( Elf_Half num ) const noexcept override { if ( num < sections.size() ) { return sections[num]; @@ -152,10 +154,13 @@ template class segment_impl : public segment } //------------------------------------------------------------------------------ - bool is_offset_initialized() const override { return is_offset_set; } + bool is_offset_initialized() const noexcept override + { + return is_offset_set; + } //------------------------------------------------------------------------------ - const std::vector& get_sections() const override + const std::vector& get_sections() const noexcept override { return sections; } @@ -166,7 +171,7 @@ template class segment_impl : public segment //------------------------------------------------------------------------------ bool load( std::istream& stream, std::streampos header_offset, - bool is_lazy_ ) override + bool is_lazy_ ) noexcept override { pstream = &stream; is_lazy = is_lazy_; @@ -191,7 +196,7 @@ template class segment_impl : public segment } //------------------------------------------------------------------------------ - bool load_data() const + bool load_data() const noexcept { if ( PT_NULL == get_type() || 0 == get_file_size() ) { return true; @@ -222,7 +227,7 @@ template class segment_impl : public segment //------------------------------------------------------------------------------ void save( std::ostream& stream, std::streampos header_offset, - std::streampos data_offset ) override + std::streampos data_offset ) noexcept override { ph.p_offset = decltype( ph.p_offset )( data_offset ); ph.p_offset = ( *convertor )( ph.p_offset ); @@ -231,15 +236,15 @@ template class segment_impl : public segment } //------------------------------------------------------------------------------ - size_t get_stream_size() const { return stream_size; } + size_t get_stream_size() const noexcept { return stream_size; } //------------------------------------------------------------------------------ - void set_stream_size( size_t value ) { stream_size = value; } + void set_stream_size( size_t value ) noexcept { stream_size = value; } //------------------------------------------------------------------------------ private: mutable std::istream* pstream = nullptr; - T ph = { 0 }; + T ph = {}; Elf_Half index = 0; mutable std::unique_ptr data; std::vector sections;