add some extra GNS-record well-formedness checks if logging is enabled
[oweals/gnunet.git] / src / gnsrecord / gnsrecord_serialization.c
index 56521945d1282b25243dc36ca5f4717c6e466331..1db27464fd2ec344f7ec4792c93d53720353f7dc 100644 (file)
@@ -127,17 +127,38 @@ GNUNET_GNSRECORD_records_serialize (unsigned int rd_count,
     rec.record_type = htonl (rd[i].record_type);
     rec.flags = htonl (rd[i].flags);
     if (off + sizeof (rec) > dest_size)
+    {
+      GNUNET_break (0);
       return -1;
+    }
     GNUNET_memcpy (&dest[off],
                    &rec,
                    sizeof (rec));
     off += sizeof (rec);
     if (off + rd[i].data_size > dest_size)
+    {
+      GNUNET_break (0);
       return -1;
+    }
     GNUNET_memcpy (&dest[off],
                    rd[i].data,
                    rd[i].data_size);
     off += rd[i].data_size;
+#if GNUNET_EXTRA_LOGGING
+    {
+      char *str;
+
+      str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
+                                              rd[i].data,
+                                              rd[i].data_size);
+      if (NULL == str)
+      {
+        GNUNET_break_op (0);
+        return GNUNET_SYSERR;
+      }
+      GNUNET_free (str);
+    }
+#endif
   }
   return off;
 }
@@ -165,7 +186,10 @@ GNUNET_GNSRECORD_records_deserialize (size_t len,
   for (unsigned int i=0;i<rd_count;i++)
   {
     if (off + sizeof (rec) > len)
+    {
+      GNUNET_break_op (0);
       return GNUNET_SYSERR;
+    }
     GNUNET_memcpy (&rec,
                    &src[off],
                    sizeof (rec));
@@ -175,9 +199,27 @@ GNUNET_GNSRECORD_records_deserialize (size_t len,
     dest[i].flags = ntohl (rec.flags);
     off += sizeof (rec);
     if (off + dest[i].data_size > len)
+    {
+      GNUNET_break_op (0);
       return GNUNET_SYSERR;
+    }
     dest[i].data = &src[off];
     off += dest[i].data_size;
+#if GNUNET_EXTRA_LOGGING
+    {
+      char *str;
+
+      str = GNUNET_GNSRECORD_value_to_string (dest[i].record_type,
+                                              dest[i].data,
+                                              dest[i].data_size);
+      if (NULL == str)
+      {
+        GNUNET_break_op (0);
+        return GNUNET_SYSERR;
+      }
+      GNUNET_free (str);
+    }
+#endif
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Deserialized record %u with flags %d and expiration time %llu\n",
          i,