check properly for mmap failures
authorChristian Grothoff <christian@grothoff.org>
Thu, 12 Nov 2009 15:45:15 +0000 (15:45 +0000)
committerChristian Grothoff <christian@grothoff.org>
Thu, 12 Nov 2009 15:45:15 +0000 (15:45 +0000)
src/util/disk.c
src/util/test_strings.c

index e280374d3ab703d6c862f4550fd083f935451f1c..ce0b2676f1433612b019e319b4282861d5689189 100644 (file)
@@ -1432,6 +1432,10 @@ struct GNUNET_DISK_MapHandle
 };
 
 
+#ifndef MAP_FAILED
+#define MAP_FAILED ((void *) -1)
+#endif
+
 /**
  * Map a file into memory
  *
@@ -1498,6 +1502,7 @@ GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
   return ret;
 #else
   int prot;
+  int ec;
 
   prot = 0;
   if (access & GNUNET_DISK_MAP_TYPE_READ)
@@ -1506,6 +1511,14 @@ GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
     prot |= PROT_WRITE;
   *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
   (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
+  GNUNET_assert (NULL != (*m)->addr);
+  if (MAP_FAILED == (*m)->addr)
+    {    
+      ec = errno;
+      GNUNET_free (*m);
+      errno = ec;
+      return NULL;
+    }
   (*m)->len = len;
   return (*m)->addr;
 #endif
index be166e629ccc715269057ed09c57009f8f2f6f10..107bb746c09479cd6b9d1725932cc2ca109820e3 100644 (file)
@@ -90,11 +90,11 @@ check ()
       GNUNET_free (r);
       return 1;
     }
+  GNUNET_free (r);
   b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "ASCII");
   WANT ("TEST", b);
   b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "unknown");
   WANT ("TEST", b);
-  GNUNET_free (r);
   return 0;
 }