Explorar o código

Throw ArgumentException for invalid combinations of FileMode and FileAccess.

Fixes issue #191.
Gert Driesen %!s(int64=8) %!d(string=hai) anos
pai
achega
40e106828d
Modificáronse 1 ficheiros con 16 adicións e 4 borrados
  1. 16 4
      src/Renci.SshNet/Sftp/SftpFileStream.cs

+ 16 - 4
src/Renci.SshNet/Sftp/SftpFileStream.cs

@@ -175,10 +175,6 @@ namespace Renci.SshNet.Sftp
                 throw new ArgumentNullException("path");
             if (bufferSize <= 0)
                 throw new ArgumentOutOfRangeException("bufferSize");
-            if (access < FileAccess.Read || access > FileAccess.ReadWrite)
-                throw new ArgumentOutOfRangeException("access");
-            if (mode < FileMode.CreateNew || mode > FileMode.Append)
-                throw new ArgumentOutOfRangeException("mode");
 
             Timeout = TimeSpan.FromSeconds(30);
             Name = path;
@@ -203,6 +199,20 @@ namespace Renci.SshNet.Sftp
                     flags |= Flags.Read;
                     flags |= Flags.Write;
                     break;
+                default:
+                    throw new ArgumentOutOfRangeException("access");
+            }
+
+            if ((access & FileAccess.Write) == 0)
+            {
+                if (mode == FileMode.Create || mode == FileMode.CreateNew || mode == FileMode.Truncate || mode == FileMode.Append)
+                {
+                    throw new ArgumentException(string.Format("Combining {0}: {1} with {2}: {3} is invalid.",
+                        typeof(FileMode).Name,
+                        mode,
+                        typeof(FileAccess).Name,
+                        access));
+                }
             }
 
             switch (mode)
@@ -232,6 +242,8 @@ namespace Renci.SshNet.Sftp
                 case FileMode.Truncate:
                     flags |= Flags.Truncate;
                     break;
+                default:
+                    throw new ArgumentOutOfRangeException("mode");
             }
 
             if (_handle == null)