瀏覽代碼

[system.core] Fix some MemoryMappedFile argument checks

Marek Safar 11 年之前
父節點
當前提交
4660f44381
共有 1 個文件被更改,包括 15 次插入6 次删除
  1. 15 6
      mcs/class/System.Core/System.IO.MemoryMappedFiles/MemoryMappedFile.cs

+ 15 - 6
mcs/class/System.Core/System.IO.MemoryMappedFiles/MemoryMappedFile.cs

@@ -117,23 +117,30 @@ namespace System.IO.MemoryMappedFiles
 			}
 		}
 
-		internal static int Open (string path, FileMode mode, long capacity, MemoryMappedFileAccess access)
+		internal static int Open (string path, FileMode mode, ref long capacity, MemoryMappedFileAccess access)
 		{
 			if (MonoUtil.IsUnix){
 				Stat buf;
 				if (Syscall.stat (path, out buf) == -1)
 					UnixMarshal.ThrowExceptionForLastError ();
 
-				if (capacity > buf.st_size)
-					throw new ArgumentException ("capacity");
+				if (capacity == 0) {
+					if (buf.st_size == 0)
+						throw new ArgumentException ("A positive capacity must be specified for a Memory Mapped File backed by an empty file.");
+
+					capacity = buf.st_size;
+				} else if (capacity < buf.st_size) {
+					throw new ArgumentException ("The capacity may not be smaller than the file size.");
+				}
 
 				int fd = Syscall.open (path, ToUnixMode (mode) | ToUnixMode (access), FilePermissions.DEFFILEMODE);
 
 				if (fd == -1)
 					UnixMarshal.ThrowExceptionForLastError ();
 				return fd;
-			} else
-				throw new NotImplementedException ();
+			}
+
+			throw new NotImplementedException ();
 		}
 		
 		internal static void CloseFD (int fd) {
@@ -481,8 +488,10 @@ namespace System.IO.MemoryMappedFiles
 				throw new ArgumentException ("mapName");
 			if (mode == FileMode.Append)
 				throw new ArgumentException ("mode");			
+			if (capacity < 0)
+				throw new ArgumentOutOfRangeException ("capacity");
 
-			int fd = MemoryMapImpl.Open (path, mode, capacity, access);
+			int fd = MemoryMapImpl.Open (path, mode, ref capacity, access);
 			
 			return new MemoryMappedFile () {
 				unix_fd = fd,