From 089160c40eeca96f836d5145af45a832d7841d2d Mon Sep 17 00:00:00 2001 From: Serge Lamikhov-Center Date: Mon, 28 Aug 2023 18:43:47 +0300 Subject: [PATCH] Use more portable function memchr() Use more portable memchr() function instead of strnlen() which is not in ISO standard --- elfio/elfio_strings.hpp | 4 ++-- elfio/elfio_utils.hpp | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/elfio/elfio_strings.hpp b/elfio/elfio_strings.hpp index 09f5729..530e037 100644 --- a/elfio/elfio_strings.hpp +++ b/elfio/elfio_strings.hpp @@ -45,8 +45,8 @@ template class string_section_accessor_template if ( string_section ) { const char* data = string_section->get_data(); if ( index < string_section->get_size() && nullptr != data ) { - size_t string_length = - strnlen( data + index, string_section->get_size() - index ); + size_t string_length = strnlength( + data + index, string_section->get_size() - index ); if ( string_length < ( string_section->get_size() - index ) ) return data + index; } diff --git a/elfio/elfio_utils.hpp b/elfio/elfio_utils.hpp index ecb0961..bd6cca8 100644 --- a/elfio/elfio_utils.hpp +++ b/elfio/elfio_utils.hpp @@ -25,6 +25,7 @@ THE SOFTWARE. #include #include +#include #define ELFIO_GET_ACCESS_DECL( TYPE, NAME ) virtual TYPE get_##NAME() const = 0 @@ -259,6 +260,13 @@ inline void adjust_stream_size( std::ostream& stream, std::streamsize offset ) stream.seekp( offset ); } +//------------------------------------------------------------------------------ +inline static size_t strnlength( const char* s, size_t n ) +{ + const char* found = (const char*)std::memchr( s, '\0', n ); + return found ? (size_t)( found - s ) : n; +} + /** * Consumers should write an implementation of this class and pass an instance of it to the ELFIO::elfio constructor. */