Asserted the encode16() and decode16() parameters, warning about off-by-one errors
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sat, 13 Aug 2011 17:23:13 +0000 (14:23 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sat, 13 Aug 2011 21:24:30 +0000 (18:24 -0300)
src/host/messagepayload.cpp

index 00c9a6f..b857b75 100644 (file)
@@ -75,6 +75,11 @@ uint16_t MessagePayload::decode16(
         const int right_byte
 ) const
 {
+    BOOST_ASSERT( ( 0 <= left_byte ) && ( left_byte < static_cast<int>(PayloadSizeInBytes) ) );
+    BOOST_ASSERT( ( 0 <= right_byte ) && ( right_byte < static_cast<int>(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<uint16_t>::max() );
@@ -98,6 +103,11 @@ void MessagePayload::encode16(
         const uint16_t value
 )
 {
+    BOOST_ASSERT( ( 0 < left_byte ) && ( left_byte < static_cast<int>(PayloadSizeInBytes) ) );
+    BOOST_ASSERT( ( 0 < right_byte ) && ( right_byte < static_cast<int>(PayloadSizeInBytes) ) );
+    BOOST_ASSERT( left_byte < right_byte );
+    BOOST_ASSERT( ( right_byte - left_byte ) == sizeof(uint16_t) );
+
     Payload[ left_byte ] = static_cast<uint8_t> ( value >> 8 );
     Payload[ right_byte ] = static_cast<uint8_t> ( 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
 {