From 1df0a8aa472b313476fe20f234e3b39cf0f78d61 Mon Sep 17 00:00:00 2001
From: Matthias Pfafferodt <git@mapfa.de>
Date: Sun, 6 Feb 2011 17:57:31 +0100
Subject: [PATCH 11/11] Add checks in dataio.c

This patch adds data checks to dataio.c to prevent bugs (like bug #15426)

see gna patch #1483
patch by pepeto
---
 common/dataio.c |   37 +++++++++++++++++++------------------
 1 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/common/dataio.c b/common/dataio.c
index 9dfcdec..6185065 100644
--- a/common/dataio.c
+++ b/common/dataio.c
@@ -64,6 +64,13 @@ static bool get_conv(char *dst, size_t ndst, const char *src,
 static DIO_PUT_CONV_FUN put_conv_callback = NULL;
 static DIO_GET_CONV_FUN get_conv_callback = get_conv;
 
+
+#ifdef DEBUG
+#define DIO_DEBUG_ASSERT(check) fc_assert(check)
+#else
+#define DIO_DEBUG_ASSERT(check) /* Nothing. */
+#endif /* DEBUG */
+
 /**************************************************************************
   Sets string conversion callback to be used when putting text.
 **************************************************************************/
@@ -199,7 +206,8 @@ void dio_put_uint8(struct data_out *dout, int value)
   if (enough_space(dout, 1)) {
     uint8_t x = value;
 
-    fc_assert(sizeof(x) == 1);
+    DIO_DEBUG_ASSERT(sizeof(x) == 1);
+    DIO_DEBUG_ASSERT(0 <= value && value < (1 << (1 * 8)));
     memcpy(ADD_TO_POINTER(dout->dest, dout->current), &x, 1);
     dout->current++;
   }
@@ -213,7 +221,8 @@ void dio_put_uint16(struct data_out *dout, int value)
   if (enough_space(dout, 2)) {
     uint16_t x = htons(value);
 
-    fc_assert(sizeof(x) == 2);
+    DIO_DEBUG_ASSERT(sizeof(x) == 2);
+    DIO_DEBUG_ASSERT(0 <= value && value < (1 << (2 * 8)));
     memcpy(ADD_TO_POINTER(dout->dest, dout->current), &x, 2);
     dout->current += 2;
   }
@@ -227,7 +236,8 @@ void dio_put_uint32(struct data_out *dout, int value)
   if (enough_space(dout, 4)) {
     uint32_t x = htonl(value);
 
-    fc_assert(sizeof(x) == 4);
+    DIO_DEBUG_ASSERT(sizeof(x) == 4);
+    DIO_DEBUG_ASSERT(0 <= value && value < (1LL << (4 * 8)));
     memcpy(ADD_TO_POINTER(dout->dest, dout->current), &x, 4);
     dout->current += 4;
   }
@@ -238,11 +248,7 @@ void dio_put_uint32(struct data_out *dout, int value)
 **************************************************************************/
 void dio_put_bool8(struct data_out *dout, bool value)
 {
-  if (value != TRUE && value != FALSE) {
-    log_error("Trying to put a non-boolean: %d", (int) value);
-    value = FALSE;
-  }
-
+  DIO_DEBUG_ASSERT(value == TRUE || value == FALSE);
   dio_put_uint8(dout, value ? 1 : 0);
 }
 
@@ -251,11 +257,7 @@ void dio_put_bool8(struct data_out *dout, bool value)
 **************************************************************************/
 void dio_put_bool32(struct data_out *dout, bool value)
 {
-  if (value != TRUE && value != FALSE) {
-    log_error("Trying to put a non-boolean: %d", (int) value);
-    value = FALSE;
-  }
-
+  DIO_DEBUG_ASSERT(value == TRUE || value == FALSE);
   dio_put_uint32(dout, value ? 1 : 0);
 }
 
@@ -349,8 +351,7 @@ void dio_put_bit_string(struct data_out *dout, const char *value)
   size_t max = (unsigned short)(-1);
 
   if (bits > max) {
-    fc_assert_msg(FALSE, "Bit string too long: %lu bits.",
-                  (unsigned long) bits);
+    log_error("Bit string too long: %lu bits.", (unsigned long) bits);
     bits = max;
   }
   bytes = (bits + 7) / 8;
@@ -416,7 +417,7 @@ void dio_get_uint8(struct data_in *din, int *dest)
     if (dest) {
       uint8_t x;
 
-      fc_assert(sizeof(x) == 1);
+      DIO_DEBUG_ASSERT(sizeof(x) == 1);
       memcpy(&x, ADD_TO_POINTER(din->src, din->current), 1);
       *dest = x;
     }
@@ -436,7 +437,7 @@ void dio_get_uint16(struct data_in *din, int *dest)
     if (dest) {
       uint16_t x;
 
-      fc_assert(sizeof(x) == 2);
+      DIO_DEBUG_ASSERT(sizeof(x) == 2);
       memcpy(&x, ADD_TO_POINTER(din->src, din->current), 2);
       *dest = ntohs(x);
     }
@@ -456,7 +457,7 @@ void dio_get_uint32(struct data_in *din, int *dest)
     if (dest) {
       uint32_t x;
 
-      fc_assert(sizeof(x) == 4);
+      DIO_DEBUG_ASSERT(sizeof(x) == 4);
       memcpy(&x, ADD_TO_POINTER(din->src, din->current), 4);
       *dest = ntohl(x);
     }
-- 
1.7.1

