Fail on mappings that exceed the file size.
authorVlad Brezae <brezaevlad@gmail.com>
Thu, 15 Jan 2015 23:52:44 +0000 (15:52 -0800)
committerVlad Brezae <brezaevlad@gmail.com>
Mon, 26 Jan 2015 23:08:14 +0000 (15:08 -0800)
As MS does.

mcs/class/System.Core/Test/System.IO.MemoryMappedFiles/MemoryMappedFileTest.cs
mono/metadata/file-mmap-posix.c

index 3905846c77174ef5cc21920a68855731a05d71cc..b0d154948d0521d27c2bedb7c30e0f7d226718fb 100644 (file)
@@ -331,7 +331,7 @@ namespace MonoTests.System.IO.MemoryMappedFiles {
                }
 
                [Test]
-               [ExpectedException(typeof(NotSupportedException))]
+               [ExpectedException(typeof(IOException))]
                public void CreateViewStreamWithOffsetPastFileEnd2 ()
                {
                        string f = Path.Combine (tempDir, "8192-file");
@@ -340,9 +340,6 @@ namespace MonoTests.System.IO.MemoryMappedFiles {
                        MemoryMappedFile mappedFile = MemoryMappedFile.CreateFromFile (f, FileMode.Open);
 
                        MemoryMappedViewStream stream = mappedFile.CreateViewStream (8191, 8191, MemoryMappedFileAccess.ReadWrite);
-
-                       /* Should throw exception due to trying to overflow capacity */
-                       stream.Write (new byte [8191], 0, 8191);
                }
        }
 }
index fcb6682fecee708c4946ba704b6b647f487259a1..6e66af6a763eac6ff42731a8eab9ce046b026e1f 100644 (file)
@@ -479,14 +479,14 @@ mono_mmap_map (void *handle, gint64 offset, gint64 *size, int access, void **mma
        struct stat buf = { 0 };
        fstat (fh->fd, &buf); //FIXME error handling
 
-       if (offset > buf.st_size)
+       if (offset > buf.st_size || ((eff_size + offset) > buf.st_size && !is_special_zero_size_file (&buf)))
                goto error;
        /**
          * We use the file size if one of the following conditions is true:
          *  -input size is zero
          *  -input size is bigger than the file and the file is not a magical zero size file such as /dev/mem.
          */
-       if (eff_size == 0 || ((eff_size + offset) > buf.st_size && !is_special_zero_size_file (&buf)))
+       if (eff_size == 0)
                eff_size = buf.st_size - offset;
        *size = eff_size;