tw.WriteLine ("Hello World!");
}
}
+
+ [Test]
+ [ExpectedException(typeof(IOException))]
+ public void CreateViewStreamWithOffsetPastFileEnd ()
+ {
+ string f = Path.Combine (tempDir, "8192-file");
+ File.WriteAllBytes (f, new byte [8192]);
+
+ MemoryMappedFile mappedFile = MemoryMappedFile.CreateFromFile (f, FileMode.Open, "myMap", 8192);
+
+ /* Should throw exception when trying to map past end of file */
+ MemoryMappedViewStream stream = mappedFile.CreateViewStream (8200, 10, MemoryMappedFileAccess.ReadWrite);
+ }
+
+ [Test]
+ [ExpectedException(typeof(NotSupportedException))]
+ public void CreateViewStreamWithOffsetPastFileEnd2 ()
+ {
+ string f = Path.Combine (tempDir, "8192-file");
+ File.WriteAllBytes (f, new byte [8192]);
+
+ 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);
+ }
}
}
struct stat buf = { 0 };
fstat (fh->fd, &buf); //FIXME error handling
+ if (offset > buf.st_size)
+ 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 > buf.st_size && !is_special_zero_size_file (&buf)))
- eff_size = buf.st_size;
+ if (eff_size == 0 || ((eff_size + offset) > buf.st_size && !is_special_zero_size_file (&buf)))
+ eff_size = buf.st_size - offset;
*size = eff_size;
mmap_offset = align_down_to_page_size (offset);
return 0;
}
+error:
*mmap_handle = NULL;
*base_address = NULL;
return COULD_NOT_MAP_MEMORY;