From: Guilherme Maciel Ferreira Date: Sat, 13 Aug 2011 17:23:13 +0000 (-0300) Subject: Asserted the encode16() and decode16() parameters, warning about off-by-one errors X-Git-Tag: v1.1^2~18 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=0b98ea6070cfbed322923beb9eb1f95fd9436313;p=pingcheck Asserted the encode16() and decode16() parameters, warning about off-by-one errors --- diff --git a/src/host/messagepayload.cpp b/src/host/messagepayload.cpp index 00c9a6f..b857b75 100644 --- a/src/host/messagepayload.cpp +++ b/src/host/messagepayload.cpp @@ -75,6 +75,11 @@ uint16_t MessagePayload::decode16( const int right_byte ) const { + BOOST_ASSERT( ( 0 <= left_byte ) && ( left_byte < static_cast(PayloadSizeInBytes) ) ); + BOOST_ASSERT( ( 0 <= right_byte ) && ( right_byte < static_cast(PayloadSizeInBytes) ) ); + BOOST_ASSERT( left_byte < right_byte ); + BOOST_ASSERT( ( right_byte - left_byte ) == sizeof(uint16_t) ); + uint32_t value = ( Payload[ left_byte ] << 8 ) + Payload[ right_byte ]; BOOST_ASSERT( value <= numeric_limits::max() ); @@ -98,6 +103,11 @@ void MessagePayload::encode16( const uint16_t value ) { + BOOST_ASSERT( ( 0 < left_byte ) && ( left_byte < static_cast(PayloadSizeInBytes) ) ); + BOOST_ASSERT( ( 0 < right_byte ) && ( right_byte < static_cast(PayloadSizeInBytes) ) ); + BOOST_ASSERT( left_byte < right_byte ); + BOOST_ASSERT( ( right_byte - left_byte ) == sizeof(uint16_t) ); + Payload[ left_byte ] = static_cast ( value >> 8 ); Payload[ right_byte ] = static_cast ( value & 0xFF ); } @@ -121,7 +131,7 @@ istream& MessagePayload::read( istream &is ) } /** - * @brief Writes all the data present in the ostream to the payload buffer. + * @brief Writes all the data from the ostream to the payload buffer. */ ostream& MessagePayload::write( ostream &os ) const {