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
This commit is contained in:
Ryan Lortie 2011-09-15 22:43:06 -04:00 committed by Matthias Clasen
parent 62ff5cd9f2
commit 817466f9a6
2 changed files with 18 additions and 1 deletions

View File

@ -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;

View File

@ -36,6 +36,18 @@ test_empty (void)
g_mapped_file_free (file);
}
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)
{
@ -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);