extra error checking in Bloom filter to check that the size of the file on disk corre...
authorChristian Grothoff <christian@grothoff.org>
Thu, 15 Dec 2011 12:40:56 +0000 (12:40 +0000)
committerChristian Grothoff <christian@grothoff.org>
Thu, 15 Dec 2011 12:40:56 +0000 (12:40 +0000)
src/include/gnunet_disk_lib.h
src/util/container_bloomfilter.c
src/util/disk.c

index 5f6a37840348bba62114b29a82f7b37032959fda..d0a9dfdb7563b940571ef9783488e24353c951c7 100644 (file)
@@ -368,6 +368,19 @@ struct GNUNET_DISK_FileHandle *
 GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
                        enum GNUNET_DISK_AccessPermissions perm);
 
+
+/**
+ * Get the size of an open file.
+ *
+ * @param fh open file handle
+ * @param size where to write size of the file
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ */
+int
+GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
+                             off_t *size);
+
+
 /**
  * Creates an interprocess channel
  * @param blocking creates an asynchronous pipe if set to GNUNET_NO
index 31e777dc360a8b6438c05aff38e0d7c88f3afe45..0349c32acc70c6ca7f0750b8ebe9d30b51c0a5d8 100644 (file)
@@ -456,6 +456,7 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
   off_t pos;
   int i;
   size_t ui;
+  off_t fsize;
 
   GNUNET_assert (NULL != filename);
   if ((k == 0) || (size == 0))
@@ -481,6 +482,21 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
     GNUNET_free (bf);
     return NULL;
   }
+  if (GNUNET_OK !=
+      GNUNET_DISK_file_handle_size (bf->fh, &fsize))
+  {
+    GNUNET_DISK_file_close (bf->fh);
+    GNUNET_free (bf);
+    return NULL;
+  }
+  if (fsize != size * 8LL)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+               _("Size of file on disk is incorrect for this Bloom filter\n"));
+    GNUNET_DISK_file_close (bf->fh);
+    GNUNET_free (bf);
+    return NULL;
+  }
   bf->filename = GNUNET_strdup (filename);
   /* Alloc block */
   bf->bitArray = GNUNET_malloc_large (size);
@@ -499,7 +515,7 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
   /* Read from the file what bits we can */
   rbuff = GNUNET_malloc (BUFFSIZE);
   pos = 0;
-  while (pos < size * 8)
+  while (pos < size * 8LL)
   {
     int res;
 
@@ -507,6 +523,11 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
     if (res == -1)
     {
       LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", bf->filename);
+      GNUNET_free (rbuff);
+      GNUNET_free (bf->filename);
+      GNUNET_DISK_file_close (bf->fh);
+      GNUNET_free (bf);
+      return NULL;
     }
     if (res == 0)
       break;                    /* is ok! we just did not use that many bits yet */
index 2bdcf3262ae0761ea2146ba6a3da58ed1a48ea41..ba5d159e410391e5c57ed428814c5f9ea8f66641 100644 (file)
@@ -205,6 +205,26 @@ GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
 }
 
 
+/**
+ * Get the size of an open file.
+ *
+ * @param fh open file handle
+ * @param size where to write size of the file
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ */
+int
+GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
+                             off_t *size)
+{
+  struct stat sbuf;
+
+  if (0 != FSTAT (fh->fd, &sbuf))
+    return GNUNET_SYSERR;
+  *size = sbuf.st_size;
+  return GNUNET_OK;
+}
+
+
 /**
  * Move the read/write pointer in a file
  *