From e391cf6841b99035bfdbffd2d272f6074cd2e97b Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 17 Sep 2022 01:09:47 +0300 Subject: [PATCH] Change hash_detail::hash_range to take and return by value to avoid It=char* aliasing --- .../container_hash/detail/hash_range.hpp | 31 ++++++++++++------- include/boost/container_hash/hash.hpp | 2 +- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/include/boost/container_hash/detail/hash_range.hpp b/include/boost/container_hash/detail/hash_range.hpp index 7450e1e..4388870 100644 --- a/include/boost/container_hash/detail/hash_range.hpp +++ b/include/boost/container_hash/detail/hash_range.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -29,21 +30,25 @@ template<> struct is_char_type: public boost::true_type {}; #endif template -inline typename boost::enable_if_< !is_char_type::value_type>::value >::type - hash_range( std::size_t& seed, It first, It last ) +inline typename boost::enable_if_< + !is_char_type::value_type>::value, +std::size_t >::type + hash_range( std::size_t seed, It first, It last ) { for( ; first != last; ++first ) { hash_combine::value_type>( seed, *first ); } + + return seed; } template inline typename boost::enable_if_< is_char_type::value_type>::value && - is_same::iterator_category, std::random_access_iterator_tag>::value ->::type - hash_range( std::size_t& seed, It first, It last ) + is_same::iterator_category, std::random_access_iterator_tag>::value, +std::size_t>::type + hash_range( std::size_t seed, It first, It last ) { std::size_t n = static_cast( last - first ); @@ -99,14 +104,16 @@ inline typename boost::enable_if_< hash_combine( seed, w ); } + + return seed; } template inline typename boost::enable_if_< is_char_type::value_type>::value && - !is_same::iterator_category, std::random_access_iterator_tag>::value ->::type - hash_range( std::size_t& seed, It first, It last ) + !is_same::iterator_category, std::random_access_iterator_tag>::value, +std::size_t>::type + hash_range( std::size_t seed, It first, It last ) { for( ;; ) { @@ -115,7 +122,7 @@ inline typename boost::enable_if_< if( first == last ) { hash_combine( seed, w | 0x01u ); - return; + return seed; } w |= static_cast( static_cast( *first ) ); @@ -124,7 +131,7 @@ inline typename boost::enable_if_< if( first == last ) { hash_combine( seed, w | 0x0100u ); - return; + return seed; } w |= static_cast( static_cast( *first ) ) << 8; @@ -133,7 +140,7 @@ inline typename boost::enable_if_< if( first == last ) { hash_combine( seed, w | 0x010000u ); - return; + return seed; } w |= static_cast( static_cast( *first ) ) << 16; @@ -142,7 +149,7 @@ inline typename boost::enable_if_< if( first == last ) { hash_combine( seed, w | 0x01000000u ); - return; + return seed; } w |= static_cast( static_cast( *first ) ) << 24; diff --git a/include/boost/container_hash/hash.hpp b/include/boost/container_hash/hash.hpp index 56bfa01..2bf8ae7 100644 --- a/include/boost/container_hash/hash.hpp +++ b/include/boost/container_hash/hash.hpp @@ -560,7 +560,7 @@ namespace boost template inline void hash_range( std::size_t& seed, It first, It last ) { - hash_detail::hash_range( seed, first, last ); + seed = hash_detail::hash_range( seed, first, last ); } template