Skip to content

Commit 40e1068

Browse files
committed
Throw ArgumentException for invalid combinations of FileMode and FileAccess.
Fixes issue #191.
1 parent 4b85e78 commit 40e1068

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/Renci.SshNet/Sftp/SftpFileStream.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,6 @@ internal SftpFileStream(ISftpSession session, string path, FileMode mode, FileAc
175175
throw new ArgumentNullException("path");
176176
if (bufferSize <= 0)
177177
throw new ArgumentOutOfRangeException("bufferSize");
178-
if (access < FileAccess.Read || access > FileAccess.ReadWrite)
179-
throw new ArgumentOutOfRangeException("access");
180-
if (mode < FileMode.CreateNew || mode > FileMode.Append)
181-
throw new ArgumentOutOfRangeException("mode");
182178

183179
Timeout = TimeSpan.FromSeconds(30);
184180
Name = path;
@@ -203,6 +199,20 @@ internal SftpFileStream(ISftpSession session, string path, FileMode mode, FileAc
203199
flags |= Flags.Read;
204200
flags |= Flags.Write;
205201
break;
202+
default:
203+
throw new ArgumentOutOfRangeException("access");
204+
}
205+
206+
if ((access & FileAccess.Write) == 0)
207+
{
208+
if (mode == FileMode.Create || mode == FileMode.CreateNew || mode == FileMode.Truncate || mode == FileMode.Append)
209+
{
210+
throw new ArgumentException(string.Format("Combining {0}: {1} with {2}: {3} is invalid.",
211+
typeof(FileMode).Name,
212+
mode,
213+
typeof(FileAccess).Name,
214+
access));
215+
}
206216
}
207217

208218
switch (mode)
@@ -232,6 +242,8 @@ internal SftpFileStream(ISftpSession session, string path, FileMode mode, FileAc
232242
case FileMode.Truncate:
233243
flags |= Flags.Truncate;
234244
break;
245+
default:
246+
throw new ArgumentOutOfRangeException("mode");
235247
}
236248

237249
if (_handle == null)

0 commit comments

Comments
 (0)