From 0b98ea6070cfbed322923beb9eb1f95fd9436313 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Sat, 13 Aug 2011 14:23:13 -0300 Subject: [PATCH] Asserted the encode16() and decode16() parameters, warning about off-by-one errors --- src/host/messagepayload.cpp | 12 +++++++++++- 1 files changed, 11 insertions(+), 1 deletions(-) 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 { -- 1.7.1