From 1eb7b9d5c66bf14585b8fc23afc99cc7b0c046b1 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Mon, 19 Mar 2018 15:49:07 +0100 Subject: [PATCH] Added some missing checks, fixed indentation, handle different meanings of max_depth --- include/fc/io/raw.hpp | 5 +++-- include/fc/reflect/variant.hpp | 2 +- include/fc/rpc/api_connection.hpp | 8 +++++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/include/fc/io/raw.hpp b/include/fc/io/raw.hpp index 30f9456..b32e440 100644 --- a/include/fc/io/raw.hpp +++ b/include/fc/io/raw.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -58,7 +59,7 @@ namespace fc { { FC_ASSERT( _max_depth > 0 ); --_max_depth; - fc::raw::pack( s, variant( msg, _max_depth ), _max_depth ); + fc::raw::pack( s, variant( msg, std::min( _max_depth, uint32_t(FC_MAX_LOG_OBJECT_DEPTH) ) ), _max_depth ); } template inline void unpack( Stream& s, fc::log_message& msg, uint32_t _max_depth ) @@ -67,7 +68,7 @@ namespace fc { fc::variant vmsg; --_max_depth; fc::raw::unpack( s, vmsg, _max_depth ); - msg = vmsg.as( _max_depth ); + msg = vmsg.as( std::min( _max_depth, uint32_t(FC_MAX_LOG_OBJECT_DEPTH) ) ); } template diff --git a/include/fc/reflect/variant.hpp b/include/fc/reflect/variant.hpp index 1a91854..60dcea8 100644 --- a/include/fc/reflect/variant.hpp +++ b/include/fc/reflect/variant.hpp @@ -65,7 +65,7 @@ namespace fc template struct if_enum - { + { template static inline void to_variant( const T& v, fc::variant& vo, uint32_t max_depth ) { diff --git a/include/fc/rpc/api_connection.hpp b/include/fc/rpc/api_connection.hpp index 0c10ee5..1345c45 100644 --- a/include/fc/rpc/api_connection.hpp +++ b/include/fc/rpc/api_connection.hpp @@ -45,13 +45,15 @@ namespace fc { R call_generic( const std::function& f, variants::const_iterator a0, variants::const_iterator e, uint32_t max_depth ) { FC_ASSERT( a0 != e ); - return call_generic( bind_first_arg( f, a0->as< typename std::decay::type >( max_depth - 1 ) ), a0+1, e, max_depth - 1 ); + FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" ); + return call_generic( bind_first_arg( f, a0->as< typename std::decay::type >( max_depth - 1 ) ), a0+1, e, max_depth - 1 ); } template std::function to_generic( const std::function& f ) { return [=]( const variants& args, uint32_t max_depth ) { + FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" ); return variant( call_generic( f, args.begin(), args.end(), max_depth - 1 ), max_depth - 1 ); }; } @@ -60,6 +62,7 @@ namespace fc { std::function to_generic( const std::function& f ) { return [=]( const variants& args, uint32_t max_depth ) { + FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" ); call_generic( f, args.begin(), args.end(), max_depth - 1 ); return variant(); }; @@ -147,6 +150,7 @@ namespace fc { R call_generic( const std::function,Args...)>& f, variants::const_iterator a0, variants::const_iterator e, uint32_t max_depth ) { FC_ASSERT( a0 != e, "too few arguments passed to method" ); + FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" ); detail::callback_functor arg0( get_connection(), a0->as(1) ); return call_generic( this->bind_first_arg,Args...>( f, std::function(arg0) ), a0+1, e, max_depth - 1 ); } @@ -154,6 +158,7 @@ namespace fc { R call_generic( const std::function&,Args...)>& f, variants::const_iterator a0, variants::const_iterator e, uint32_t max_depth ) { FC_ASSERT( a0 != e, "too few arguments passed to method" ); + FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" ); detail::callback_functor arg0( get_connection(), a0->as(1) ); return call_generic( this->bind_first_arg&,Args...>( f, arg0 ), a0+1, e, max_depth - 1 ); } @@ -162,6 +167,7 @@ namespace fc { R call_generic( const std::function& f, variants::const_iterator a0, variants::const_iterator e, uint32_t max_depth ) { FC_ASSERT( a0 != e, "too few arguments passed to method" ); + FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" ); return call_generic( this->bind_first_arg( f, a0->as< typename std::decay::type >( max_depth - 1 ) ), a0+1, e, max_depth - 1 ); }