Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 23f8d69

Browse files
committed
Ensure we set ParamName of ArgumentException where applicable.
[tfs-changeset: 1493461]
1 parent 47cb2a5 commit 23f8d69

File tree

13 files changed

+46
-74
lines changed

13 files changed

+46
-74
lines changed

src/System.IO.FileSystem/src/Resources/Strings.resx

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,6 @@
144144
<data name="Arg_PathIsVolume" xml:space="preserve">
145145
<value>Path must not be a drive.</value>
146146
</data>
147-
<data name="Arg_WrongAsyncResult" xml:space="preserve">
148-
<value>IAsyncResult object did not come from the corresponding async method on this type.</value>
149-
</data>
150147
<data name="ArgumentNull_Buffer" xml:space="preserve">
151148
<value>Buffer cannot be null.</value>
152149
</data>
@@ -267,18 +264,6 @@
267264
<data name="IndexOutOfRange_IORaceCondition" xml:space="preserve">
268265
<value>Probable I/O race condition detected while copying memory. The I/O package is not thread safe by default. In multithreaded applications, a stream must be accessed in a thread-safe way, such as a thread-safe wrapper returned by TextReader's or TextWriter's Synchronized methods. This also applies to classes like StreamWriter and StreamReader.</value>
269266
</data>
270-
<data name="InvalidOperation_EndReadCalledMultiple" xml:space="preserve">
271-
<value>EndRead can only be called once for each asynchronous operation.</value>
272-
</data>
273-
<data name="InvalidOperation_EndWriteCalledMultiple" xml:space="preserve">
274-
<value>EndWrite can only be called once for each asynchronous operation.</value>
275-
</data>
276-
<data name="InvalidOperation_WrongAsyncResultOrEndReadCalledMultiple" xml:space="preserve">
277-
<value>Either the IAsyncResult object did not come from the corresponding async method on this type, or EndRead was called multiple times with the same IAsyncResult.</value>
278-
</data>
279-
<data name="InvalidOperation_WrongAsyncResultOrEndWriteCalledMultiple" xml:space="preserve">
280-
<value>Either the IAsyncResult object did not come from the corresponding async method on this type, or EndWrite was called multiple times with the same IAsyncResult.</value>
281-
</data>
282267
<data name="NotSupported_FileStreamOnNonFiles" xml:space="preserve">
283268
<value>FileStream was asked to open a device that was not a file. For support for devices like 'com1:' or 'lpt1:', call CreateFile, then use the FileStream constructors that take an OS handle as an IntPtr.</value>
284269
</data>

src/System.IO.FileSystem/src/System/IO/Directory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static DirectoryInfo CreateDirectory(String path)
4141
if (path == null)
4242
throw new ArgumentNullException("path");
4343
if (path.Length == 0)
44-
throw new ArgumentException(SR.Argument_PathEmpty);
44+
throw new ArgumentException(SR.Argument_PathEmpty, "path");
4545
Contract.EndContractBlock();
4646

4747
String fullPath = PathHelpers.GetFullPathInternal(path);
@@ -540,7 +540,7 @@ public static void SetCurrentDirectory(String path)
540540
if (path == null)
541541
throw new ArgumentNullException("value");
542542
if (path.Length == 0)
543-
throw new ArgumentException(SR.Argument_PathEmpty);
543+
throw new ArgumentException(SR.Argument_PathEmpty, "path");
544544
Contract.EndContractBlock();
545545
if (path.Length >= FileSystem.Current.MaxPath)
546546
throw new PathTooLongException(SR.IO_PathTooLong);

src/System.IO.FileSystem/src/System/IO/DirectoryInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private DirectoryInfo CreateSubdirectoryHelper(String path)
111111

112112
if (0 != String.Compare(FullPath, 0, fullPath, 0, FullPath.Length, PathInternal.GetComparison()))
113113
{
114-
throw new ArgumentException(SR.Format(SR.Argument_InvalidSubPath, path, DisplayPath));
114+
throw new ArgumentException(SR.Format(SR.Argument_InvalidSubPath, path, DisplayPath), "path");
115115
}
116116

117117
FileSystem.Current.CreateDirectory(fullPath);

src/System.IO.FileSystem/src/System/IO/File.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public static String ReadAllText(String path)
355355
if (path == null)
356356
throw new ArgumentNullException("path");
357357
if (path.Length == 0)
358-
throw new ArgumentException(SR.Argument_EmptyPath);
358+
throw new ArgumentException(SR.Argument_EmptyPath, "path");
359359
Contract.EndContractBlock();
360360

361361
return InternalReadAllText(path, Encoding.UTF8);
@@ -369,7 +369,7 @@ public static String ReadAllText(String path, Encoding encoding)
369369
if (encoding == null)
370370
throw new ArgumentNullException("encoding");
371371
if (path.Length == 0)
372-
throw new ArgumentException(SR.Argument_EmptyPath);
372+
throw new ArgumentException(SR.Argument_EmptyPath, "path");
373373
Contract.EndContractBlock();
374374

375375
return InternalReadAllText(path, encoding);
@@ -394,7 +394,7 @@ public static void WriteAllText(String path, String contents)
394394
if (path == null)
395395
throw new ArgumentNullException("path");
396396
if (path.Length == 0)
397-
throw new ArgumentException(SR.Argument_EmptyPath);
397+
throw new ArgumentException(SR.Argument_EmptyPath, "path");
398398
Contract.EndContractBlock();
399399

400400
InternalWriteAllText(path, contents, UTF8NoBOM);
@@ -408,7 +408,7 @@ public static void WriteAllText(String path, String contents, Encoding encoding)
408408
if (encoding == null)
409409
throw new ArgumentNullException("encoding");
410410
if (path.Length == 0)
411-
throw new ArgumentException(SR.Argument_EmptyPath);
411+
throw new ArgumentException(SR.Argument_EmptyPath, "path");
412412
Contract.EndContractBlock();
413413

414414
InternalWriteAllText(path, contents, encoding);
@@ -464,7 +464,7 @@ public static void WriteAllBytes(String path, byte[] bytes)
464464
if (path == null)
465465
throw new ArgumentNullException("path", SR.ArgumentNull_Path);
466466
if (path.Length == 0)
467-
throw new ArgumentException(SR.Argument_EmptyPath);
467+
throw new ArgumentException(SR.Argument_EmptyPath, "path");
468468
if (bytes == null)
469469
throw new ArgumentNullException("bytes");
470470
Contract.EndContractBlock();
@@ -489,7 +489,7 @@ public static String[] ReadAllLines(String path)
489489
if (path == null)
490490
throw new ArgumentNullException("path");
491491
if (path.Length == 0)
492-
throw new ArgumentException(SR.Argument_EmptyPath);
492+
throw new ArgumentException(SR.Argument_EmptyPath, "path");
493493
Contract.EndContractBlock();
494494

495495
return InternalReadAllLines(path, Encoding.UTF8);
@@ -502,7 +502,7 @@ public static String[] ReadAllLines(String path, Encoding encoding)
502502
if (encoding == null)
503503
throw new ArgumentNullException("encoding");
504504
if (path.Length == 0)
505-
throw new ArgumentException(SR.Argument_EmptyPath);
505+
throw new ArgumentException(SR.Argument_EmptyPath, "path");
506506
Contract.EndContractBlock();
507507

508508
return InternalReadAllLines(path, encoding);
@@ -557,7 +557,7 @@ public static void WriteAllLines(String path, IEnumerable<String> contents)
557557
if (contents == null)
558558
throw new ArgumentNullException("contents");
559559
if (path.Length == 0)
560-
throw new ArgumentException(SR.Argument_EmptyPath);
560+
throw new ArgumentException(SR.Argument_EmptyPath, "path");
561561
Contract.EndContractBlock();
562562

563563
Stream stream = FileStream.InternalCreate(path, useAsync: false);
@@ -574,7 +574,7 @@ public static void WriteAllLines(String path, IEnumerable<String> contents, Enco
574574
if (encoding == null)
575575
throw new ArgumentNullException("encoding");
576576
if (path.Length == 0)
577-
throw new ArgumentException(SR.Argument_EmptyPath);
577+
throw new ArgumentException(SR.Argument_EmptyPath, "path");
578578
Contract.EndContractBlock();
579579

580580
Stream stream = FileStream.InternalCreate(path, useAsync: false);
@@ -602,7 +602,7 @@ public static void AppendAllText(String path, String contents)
602602
if (path == null)
603603
throw new ArgumentNullException("path");
604604
if (path.Length == 0)
605-
throw new ArgumentException(SR.Argument_EmptyPath);
605+
throw new ArgumentException(SR.Argument_EmptyPath, "path");
606606
Contract.EndContractBlock();
607607

608608
InternalAppendAllText(path, contents, UTF8NoBOM);
@@ -615,7 +615,7 @@ public static void AppendAllText(String path, String contents, Encoding encoding
615615
if (encoding == null)
616616
throw new ArgumentNullException("encoding");
617617
if (path.Length == 0)
618-
throw new ArgumentException(SR.Argument_EmptyPath);
618+
throw new ArgumentException(SR.Argument_EmptyPath, "path");
619619
Contract.EndContractBlock();
620620

621621
InternalAppendAllText(path, contents, encoding);
@@ -640,7 +640,7 @@ public static void AppendAllLines(String path, IEnumerable<String> contents)
640640
if (contents == null)
641641
throw new ArgumentNullException("contents");
642642
if (path.Length == 0)
643-
throw new ArgumentException(SR.Argument_EmptyPath);
643+
throw new ArgumentException(SR.Argument_EmptyPath, "path");
644644
Contract.EndContractBlock();
645645

646646
Stream stream = FileStream.InternalAppend(path, useAsync: false);
@@ -657,7 +657,7 @@ public static void AppendAllLines(String path, IEnumerable<String> contents, Enc
657657
if (encoding == null)
658658
throw new ArgumentNullException("encoding");
659659
if (path.Length == 0)
660-
throw new ArgumentException(SR.Argument_EmptyPath);
660+
throw new ArgumentException(SR.Argument_EmptyPath, "path");
661661
Contract.EndContractBlock();
662662

663663
Stream stream = FileStream.InternalAppend(path, useAsync: false);

src/System.IO.FileSystem/src/System/IO/FileStream.Win32.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ public FileStream(Microsoft.Win32.SafeHandles.SafeFileHandle handle, FileAccess
1919
this._innerStream = new Win32FileStream(handle, access, bufferSize, isAsync, this);
2020
}
2121

22-
static partial void ValidatePath(string fullPath)
22+
static partial void ValidatePath(string fullPath, string paramName)
2323
{
2424
// Prevent access to your disk drives as raw block devices.
2525
if (fullPath.StartsWith("\\\\.\\", StringComparison.Ordinal))
26-
throw new ArgumentException(SR.Arg_DevicesNotSupported);
26+
throw new ArgumentException(SR.Arg_DevicesNotSupported, paramName);
2727

2828
// Check for additional invalid characters. Most invalid characters were checked above
2929
// in our call to Path.GetFullPath(path);
3030
if (fullPath.IndexOfAny(s_additionalInvalidChars) != -1)
31-
throw new ArgumentException(SR.Argument_InvalidPathChars);
31+
throw new ArgumentException(SR.Argument_InvalidPathChars, paramName);
3232

3333
if (fullPath.IndexOf(':', 2) != -1)
3434
throw new NotSupportedException(SR.Argument_PathFormatNotSupported);

src/System.IO.FileSystem/src/System/IO/FileStream.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,22 @@ private void Init(String path, FileMode mode, FileAccess access, FileShare share
8989
{
9090
if (mode == FileMode.Truncate || mode == FileMode.CreateNew || mode == FileMode.Create || mode == FileMode.Append)
9191
{
92-
// No write access
93-
throw new ArgumentException(SR.Format(SR.Argument_InvalidFileModeAndAccessCombo, mode, access));
92+
// No write access, mode and access disagree but flag access since mode comes first
93+
throw new ArgumentException(SR.Format(SR.Argument_InvalidFileModeAndAccessCombo, mode, access), "access");
9494
}
9595
}
9696

9797
string fullPath = PathHelpers.GetFullPathInternal(path);
9898

99-
ValidatePath(fullPath);
99+
ValidatePath(fullPath, "path");
100100

101101
if ((access & FileAccess.Read) != 0 && mode == FileMode.Append)
102-
throw new ArgumentException(SR.Argument_InvalidAppendMode);
102+
throw new ArgumentException(SR.Argument_InvalidAppendMode, "access");
103103

104104
this._innerStream = FileSystem.Current.Open(fullPath, mode, access, share, bufferSize, options, this);
105105
}
106106

107-
static partial void ValidatePath(string fullPath);
107+
static partial void ValidatePath(string fullPath, string paramName);
108108

109109
// InternalOpen, InternalCreate, and InternalAppend:
110110
// Factory methods for FileStream used by File, FileInfo, and ReadLinesIterator
@@ -230,7 +230,7 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel
230230
if (count < 0)
231231
throw new ArgumentOutOfRangeException("count", SR.ArgumentOutOfRange_NeedNonNegNum);
232232
if (buffer.Length - offset < count)
233-
throw new ArgumentException(SR.Argument_InvalidOffLen);
233+
throw new ArgumentException(SR.Argument_InvalidOffLen /*, no good single parameter name to pass*/);
234234
Contract.EndContractBlock();
235235

236236
// If we have been inherited into a subclass, the following implementation could be incorrect
@@ -272,7 +272,7 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
272272
if (count < 0)
273273
throw new ArgumentOutOfRangeException("count", SR.ArgumentOutOfRange_NeedNonNegNum);
274274
if (buffer.Length - offset < count)
275-
throw new ArgumentException(SR.Argument_InvalidOffLen);
275+
throw new ArgumentException(SR.Argument_InvalidOffLen /*, no good single parameter name to pass*/);
276276
Contract.EndContractBlock();
277277

278278
// If we have been inherited into a subclass, the following implementation could be incorrect

src/System.IO.FileSystem/src/System/IO/PathHelpers.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ internal static void CheckSearchPattern(String searchPattern)
6767
while ((index = searchPattern.IndexOf("..", index, StringComparison.Ordinal)) != -1)
6868
{
6969
if (index + 2 == searchPattern.Length) // Terminal ".." . Files names cannot end in ".."
70-
throw new ArgumentException(SR.Arg_InvalidSearchPattern);
70+
throw new ArgumentException(SR.Arg_InvalidSearchPattern, "searchPattern");
7171

7272
if (IsDirectorySeparator(searchPattern[index + 2]))
73-
throw new ArgumentException(SR.Arg_InvalidSearchPattern);
73+
throw new ArgumentException(SR.Arg_InvalidSearchPattern, "searchPattern");
7474

7575
index += 2;
7676
}
@@ -82,7 +82,7 @@ internal static void CheckInvalidPathChars(String path, bool checkAdditional = f
8282
throw new ArgumentNullException("path");
8383

8484
if (PathInternal.HasIllegalCharacters(path, checkAdditional))
85-
throw new ArgumentException(SR.Argument_InvalidPathChars);
85+
throw new ArgumentException(SR.Argument_InvalidPathChars, "path");
8686
}
8787

8888
// System.IO.Path has both public Combine and internal InternalCombine

src/System.IO.FileSystem/src/System/IO/UnixFileStream.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ internal UnixFileStream(SafeFileHandle handle, FileAccess access, int bufferSize
136136
if (bufferSize <= 0)
137137
throw new ArgumentOutOfRangeException("bufferSize", SR.ArgumentOutOfRange_NeedNonNegNum);
138138
if (handle.IsAsync.HasValue && useAsyncIO != handle.IsAsync.Value)
139-
throw new ArgumentException(SR.Arg_HandleNotAsync);
139+
throw new ArgumentException(SR.Arg_HandleNotAsync, "handle");
140140

141141
_fileHandle = handle;
142142
_access = access;
@@ -912,7 +912,7 @@ private void ValidateReadWriteArgs(byte[] array, int offset, int count)
912912
}
913913
if (array.Length - offset < count)
914914
{
915-
throw new ArgumentException(SR.Argument_InvalidOffLen);
915+
throw new ArgumentException(SR.Argument_InvalidOffLen /*, no good single parameter name to pass*/);
916916
}
917917
if (_fileHandle.IsClosed)
918918
{

src/System.IO.FileSystem/src/System/IO/UnixFileSystemObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public FileAttributes Attributes
7676
FileAttributes.System | FileAttributes.Temporary;
7777
if ((value & ~allValidFlags) != 0)
7878
{
79-
throw new ArgumentException(SR.Arg_InvalidFileAttrs);
79+
throw new ArgumentException(SR.Arg_InvalidFileAttrs, "value");
8080
}
8181

8282
// The only thing we can reasonably change is whether the file object is readonly,

src/System.IO.FileSystem/src/System/IO/Win32FileStream.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ private Win32FileStream(SafeFileHandle handle, FileAccess access, int bufferSize
276276
{
277277
// If you passed in a synchronous handle and told us to use
278278
// it asynchronously, throw here.
279-
throw new ArgumentException(SR.Arg_HandleNotAsync, ex);
279+
throw new ArgumentException(SR.Arg_HandleNotAsync, "handle", ex);
280280
}
281281
}
282282
else if (!_isAsync)
@@ -293,7 +293,7 @@ private Win32FileStream(SafeFileHandle handle, FileAccess access, int bufferSize
293293
if (isAsync)
294294
{
295295
// Passed in a synchronous handle and told us isAsync
296-
throw new ArgumentException(SR.Arg_HandleNotAsync);
296+
throw new ArgumentException(SR.Arg_HandleNotAsync, "handle");
297297
}
298298
#endif
299299

@@ -367,7 +367,7 @@ private unsafe void VerifyHandleIsSync()
367367
}
368368

369369
if (errorCode == ERROR_INVALID_PARAMETER)
370-
throw new ArgumentException(SR.Arg_HandleNotSync);
370+
throw new ArgumentException(SR.Arg_HandleNotSync, "handle");
371371
if (errorCode == Interop.mincore.Errors.ERROR_INVALID_HANDLE)
372372
throw Win32Marshal.GetExceptionForWin32Error(errorCode, "<OS handle>");
373373
}
@@ -690,7 +690,7 @@ public override int Read([In, Out] byte[] array, int offset, int count)
690690
if (count < 0)
691691
throw new ArgumentOutOfRangeException("count", SR.ArgumentOutOfRange_NeedNonNegNum);
692692
if (array.Length - offset < count)
693-
throw new ArgumentException(SR.Argument_InvalidOffLen);
693+
throw new ArgumentException(SR.Argument_InvalidOffLen /*, no good single parameter name to pass*/);
694694
Contract.EndContractBlock();
695695

696696
if (_handle.IsClosed) throw __Error.GetFileNotOpen();
@@ -794,7 +794,7 @@ private unsafe int ReadCore(byte[] buffer, int offset, int count)
794794
else
795795
{
796796
if (errorCode == ERROR_INVALID_PARAMETER)
797-
throw new ArgumentException(SR.Arg_HandleNotSync);
797+
throw new ArgumentException(SR.Arg_HandleNotSync, "handle");
798798

799799
throw Win32Marshal.GetExceptionForWin32Error(errorCode);
800800
}
@@ -972,7 +972,7 @@ public override void Write(byte[] array, int offset, int count)
972972
if (count < 0)
973973
throw new ArgumentOutOfRangeException("count", SR.ArgumentOutOfRange_NeedNonNegNum);
974974
if (array.Length - offset < count)
975-
throw new ArgumentException(SR.Argument_InvalidOffLen);
975+
throw new ArgumentException(SR.Argument_InvalidOffLen /*, no good single parameter name to pass*/);
976976
Contract.EndContractBlock();
977977

978978
if (_handle.IsClosed) throw __Error.GetFileNotOpen();

0 commit comments

Comments
 (0)