[glib/glib-2-30: 36/41] GMappedFile: fail when mapping a device file



commit 817466f9a62105b7640f9cb55bff5f92796851df
Author: Ryan Lortie <desrt desrt ca>
Date:   Thu Sep 15 22:43:06 2011 -0400

    GMappedFile: fail when mapping a device file
    
    mmap() fails on zero-sized files, so we previously had a special case to
    avoid calling it in that case.  Unfortunately, this had the side effect
    of causing us to fail to notice that we were attempting to mmap() a
    device node.
    
    Modify the special-casing to only apply in the case that we're dealing
    with a normal file.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=659212

 glib/gmappedfile.c      |    6 +++++-
 glib/tests/mappedfile.c |   13 +++++++++++++
 2 files changed, 18 insertions(+), 1 deletions(-)
---
diff --git a/glib/gmappedfile.c b/glib/gmappedfile.c
index 48081dc..a02747c 100644
--- a/glib/gmappedfile.c
+++ b/glib/gmappedfile.c
@@ -173,7 +173,11 @@ g_mapped_file_new (const gchar  *filename,
       goto out;
     }
 
-  if (st.st_size == 0)
+  /* mmap() on size 0 will fail with EINVAL, so we avoid calling mmap()
+   * in that case -- but only if we have a regular file; we still want
+   * attempts to mmap a character device to fail, for example.
+   */
+  if (st.st_size == 0 && S_ISREG (st.st_mode))
     {
       file->length = 0;
       file->contents = NULL;
diff --git a/glib/tests/mappedfile.c b/glib/tests/mappedfile.c
index 3febfc6..994af13 100644
--- a/glib/tests/mappedfile.c
+++ b/glib/tests/mappedfile.c
@@ -37,6 +37,18 @@ test_empty (void)
 }
 
 static void
+test_device (void)
+{
+  GError *error = NULL;
+  GMappedFile *file;
+
+  file = g_mapped_file_new ("/dev/null", FALSE, &error);
+  g_assert (file == NULL);
+  g_assert (error != NULL);
+  g_error_free (error);
+}
+
+static void
 test_nonexisting (void)
 {
   GMappedFile *file;
@@ -93,6 +105,7 @@ main (int argc, char *argv[])
 
   g_test_add_func ("/mappedfile/basic", test_basic);
   g_test_add_func ("/mappedfile/empty", test_empty);
+  g_test_add_func ("/mappedfile/device", test_device);
   g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
   g_test_add_func ("/mappedfile/writable", test_writable);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]