From 469940932199d4f281896184c8fd49daffdd1a5d Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Mon, 21 Aug 2017 22:25:35 -0700 Subject: [PATCH] Remove empty try's (#13493) * Remove empty try's * Remove some dead comments * more --- src/mscorlib/shared/System/DateTime.cs | 6 +- src/mscorlib/shared/System/DateTimeOffset.cs | 6 +- .../shared/System/IO/FileStream.Windows.cs | 5 +- src/mscorlib/src/Microsoft/Win32/RegistryKey.cs | 4 +- src/mscorlib/src/System/Diagnostics/Stacktrace.cs | 3 - src/mscorlib/src/System/Environment.cs | 54 ++++++++--------- .../System/Reflection/RuntimeConstructorInfo.cs | 3 +- .../Runtime/InteropServices/CriticalHandle.cs | 8 +-- .../System/Runtime/InteropServices/SafeBuffer.cs | 13 ++--- src/mscorlib/src/System/Runtime/MemoryFailPoint.cs | 46 ++++++--------- .../Runtime/Reliability/CriticalFinalizerObject.cs | 1 - src/mscorlib/src/System/Threading/ThreadPool.cs | 67 ++++++++++------------ 12 files changed, 80 insertions(+), 136 deletions(-) diff --git a/src/mscorlib/shared/System/DateTime.cs b/src/mscorlib/shared/System/DateTime.cs index 6467ffa..e72654a 100644 --- a/src/mscorlib/shared/System/DateTime.cs +++ b/src/mscorlib/shared/System/DateTime.cs @@ -1025,11 +1025,7 @@ namespace System } // Returns a DateTime representing the current date and time. The - // resolution of the returned value depends on the system timer. For - // Windows NT 3.5 and later the timer resolution is approximately 10ms, - // for Windows NT 3.1 it is approximately 16ms, and for Windows 95 and 98 - // it is approximately 55ms. - // + // resolution of the returned value depends on the system timer. public static DateTime Now { get diff --git a/src/mscorlib/shared/System/DateTimeOffset.cs b/src/mscorlib/shared/System/DateTimeOffset.cs index ab35bdb..e3366e2 100644 --- a/src/mscorlib/shared/System/DateTimeOffset.cs +++ b/src/mscorlib/shared/System/DateTimeOffset.cs @@ -129,11 +129,7 @@ namespace System } // Returns a DateTimeOffset representing the current date and time. The - // resolution of the returned value depends on the system timer. For - // Windows NT 3.5 and later the timer resolution is approximately 10ms, - // for Windows NT 3.1 it is approximately 16ms, and for Windows 95 and 98 - // it is approximately 55ms. - // + // resolution of the returned value depends on the system timer. public static DateTimeOffset Now { get diff --git a/src/mscorlib/shared/System/IO/FileStream.Windows.cs b/src/mscorlib/shared/System/IO/FileStream.Windows.cs index 9c88502..9159b03 100644 --- a/src/mscorlib/shared/System/IO/FileStream.Windows.cs +++ b/src/mscorlib/shared/System/IO/FileStream.Windows.cs @@ -742,9 +742,8 @@ namespace System.IO else { // ERROR_INVALID_PARAMETER may be returned for writes - // where the position is too large (i.e. writing at Int64.MaxValue - // on Win9x) OR for synchronous writes to a handle opened - // asynchronously. + // where the position is too large or for synchronous writes + // to a handle opened asynchronously. if (errorCode == ERROR_INVALID_PARAMETER) throw new IOException(SR.IO_FileTooLongOrHandleNotSync); throw Win32Marshal.GetExceptionForWin32Error(errorCode); diff --git a/src/mscorlib/src/Microsoft/Win32/RegistryKey.cs b/src/mscorlib/src/Microsoft/Win32/RegistryKey.cs index 2228c1f..bef0c38 100644 --- a/src/mscorlib/src/Microsoft/Win32/RegistryKey.cs +++ b/src/mscorlib/src/Microsoft/Win32/RegistryKey.cs @@ -458,9 +458,7 @@ namespace Microsoft.Win32 * Note that name can be null or "", at which point the * unnamed or default value of this Registry key is returned, if any. * The default values for RegistryKeys are OS-dependent. NT doesn't - * have them by default, but they can exist and be of any type. On - * Win95, the default value is always an empty key of type REG_SZ. - * Win98 supports default values of any type, but defaults to REG_SZ. + * have them by default, but they can exist and be of any type. * * @param name Name of value to retrieve. * @param defaultValue Value to return if name doesn't exist. diff --git a/src/mscorlib/src/System/Diagnostics/Stacktrace.cs b/src/mscorlib/src/System/Diagnostics/Stacktrace.cs index 6a138de..097ed44 100644 --- a/src/mscorlib/src/System/Diagnostics/Stacktrace.cs +++ b/src/mscorlib/src/System/Diagnostics/Stacktrace.cs @@ -247,9 +247,6 @@ namespace System.Diagnostics // Class which represents a description of a stack trace // There is no good reason for the methods of this class to be virtual. - // In order to ensure trusted code can trust the data it gets from a - // StackTrace, we use an InheritanceDemand to prevent partially-trusted - // subclasses. public class StackTrace { private StackFrame[] frames; diff --git a/src/mscorlib/src/System/Environment.cs b/src/mscorlib/src/System/Environment.cs index fe3729f..7cfd4db 100644 --- a/src/mscorlib/src/System/Environment.cs +++ b/src/mscorlib/src/System/Environment.cs @@ -241,44 +241,38 @@ namespace System { char[] block = null; - // Make sure pStrings is not leaked with async exceptions RuntimeHelpers.PrepareConstrainedRegions(); + + char* pStrings = null; + try { - } - finally - { - char* pStrings = null; - - try + pStrings = Win32Native.GetEnvironmentStrings(); + if (pStrings == null) { - pStrings = Win32Native.GetEnvironmentStrings(); - if (pStrings == null) - { - throw new OutOfMemoryException(); - } + throw new OutOfMemoryException(); + } - // Format for GetEnvironmentStrings is: - // [=HiddenVar=value\0]* [Variable=value\0]* \0 - // See the description of Environment Blocks in MSDN's - // CreateProcess page (null-terminated array of null-terminated strings). + // Format for GetEnvironmentStrings is: + // [=HiddenVar=value\0]* [Variable=value\0]* \0 + // See the description of Environment Blocks in MSDN's + // CreateProcess page (null-terminated array of null-terminated strings). - // Search for terminating \0\0 (two unicode \0's). - char* p = pStrings; - while (!(*p == '\0' && *(p + 1) == '\0')) - p++; + // Search for terminating \0\0 (two unicode \0's). + char* p = pStrings; + while (!(*p == '\0' && *(p + 1) == '\0')) + p++; - int len = (int)(p - pStrings + 1); - block = new char[len]; + int len = (int)(p - pStrings + 1); + block = new char[len]; - fixed (char* pBlock = block) - string.wstrcpy(pBlock, pStrings, len); - } - finally - { - if (pStrings != null) - Win32Native.FreeEnvironmentStrings(pStrings); - } + fixed (char* pBlock = block) + string.wstrcpy(pBlock, pStrings, len); + } + finally + { + if (pStrings != null) + Win32Native.FreeEnvironmentStrings(pStrings); } return block; diff --git a/src/mscorlib/src/System/Reflection/RuntimeConstructorInfo.cs b/src/mscorlib/src/System/Reflection/RuntimeConstructorInfo.cs index 6be5578..d201e12 100644 --- a/src/mscorlib/src/System/Reflection/RuntimeConstructorInfo.cs +++ b/src/mscorlib/src/System/Reflection/RuntimeConstructorInfo.cs @@ -57,8 +57,7 @@ namespace System.Reflection // this should be an invocable method, determine the other flags that participate in invocation invocationFlags |= RuntimeMethodHandle.GetSecurityFlags(this); - // Check for attempt to create a delegate class, we demand unmanaged - // code permission for this since it's hard to validate the target address. + // Check for attempt to create a delegate class. if (typeof(Delegate).IsAssignableFrom(DeclaringType)) invocationFlags |= INVOCATION_FLAGS.INVOCATION_FLAGS_IS_DELEGATE_CTOR; } diff --git a/src/mscorlib/src/System/Runtime/InteropServices/CriticalHandle.cs b/src/mscorlib/src/System/Runtime/InteropServices/CriticalHandle.cs index 734a494..11873bc 100644 --- a/src/mscorlib/src/System/Runtime/InteropServices/CriticalHandle.cs +++ b/src/mscorlib/src/System/Runtime/InteropServices/CriticalHandle.cs @@ -130,13 +130,7 @@ using System.IO; namespace System.Runtime.InteropServices { - // This class should not be serializable - it's a handle. We require unmanaged - // code permission to subclass CriticalHandle to prevent people from writing a - // subclass and suddenly being able to run arbitrary native code with the - // same signature as CloseHandle. This is technically a little redundant, but - // we'll do this to ensure we've cut off all attack vectors. Similarly, all - // methods have a link demand to ensure untrusted code cannot directly edit - // or alter a handle. + // This class should not be serializable - it's a handle public abstract class CriticalHandle : CriticalFinalizerObject, IDisposable { // ! Do not add or rearrange fields as the EE depends on this layout. diff --git a/src/mscorlib/src/System/Runtime/InteropServices/SafeBuffer.cs b/src/mscorlib/src/System/Runtime/InteropServices/SafeBuffer.cs index 73a3721..e7a3166 100644 --- a/src/mscorlib/src/System/Runtime/InteropServices/SafeBuffer.cs +++ b/src/mscorlib/src/System/Runtime/InteropServices/SafeBuffer.cs @@ -173,15 +173,10 @@ namespace System.Runtime.InteropServices pointer = null; RuntimeHelpers.PrepareConstrainedRegions(); - try - { - } - finally - { - bool junk = false; - DangerousAddRef(ref junk); - pointer = (byte*)handle; - } + + bool junk = false; + DangerousAddRef(ref junk); + pointer = (byte*)handle; } public void ReleasePointer() diff --git a/src/mscorlib/src/System/Runtime/MemoryFailPoint.cs b/src/mscorlib/src/System/Runtime/MemoryFailPoint.cs index f9f87bc..430574b 100644 --- a/src/mscorlib/src/System/Runtime/MemoryFailPoint.cs +++ b/src/mscorlib/src/System/Runtime/MemoryFailPoint.cs @@ -241,24 +241,20 @@ namespace System.Runtime // Attempt to grow the OS's page file. Note that we ignore // any allocation routines from the host intentionally. RuntimeHelpers.PrepareConstrainedRegions(); - try - { - } - finally + + // This shouldn't overflow due to the if clauses above. + UIntPtr numBytes = new UIntPtr(segmentSize); + unsafe { - // This shouldn't overflow due to the if clauses above. - UIntPtr numBytes = new UIntPtr(segmentSize); - unsafe + void* pMemory = Win32Native.VirtualAlloc(null, numBytes, Win32Native.MEM_COMMIT, Win32Native.PAGE_READWRITE); + if (pMemory != null) { - void* pMemory = Win32Native.VirtualAlloc(null, numBytes, Win32Native.MEM_COMMIT, Win32Native.PAGE_READWRITE); - if (pMemory != null) - { - bool r = Win32Native.VirtualFree(pMemory, UIntPtr.Zero, Win32Native.MEM_RELEASE); - if (!r) - __Error.WinIOError(); - } + bool r = Win32Native.VirtualFree(pMemory, UIntPtr.Zero, Win32Native.MEM_RELEASE); + if (!r) + __Error.WinIOError(); } } + continue; case 2: @@ -304,14 +300,9 @@ namespace System.Runtime CheckForFreeAddressSpace(segmentSize, true); RuntimeHelpers.PrepareConstrainedRegions(); - try - { - } - finally - { - SharedStatics.AddMemoryFailPointReservation((long)size); - _mustSubtractReservation = true; - } + + SharedStatics.AddMemoryFailPointReservation((long)size); + _mustSubtractReservation = true; #endif } @@ -414,14 +405,9 @@ namespace System.Runtime if (_mustSubtractReservation) { RuntimeHelpers.PrepareConstrainedRegions(); - try - { - } - finally - { - SharedStatics.AddMemoryFailPointReservation(-((long)_reservedMemory)); - _mustSubtractReservation = false; - } + + SharedStatics.AddMemoryFailPointReservation(-((long)_reservedMemory)); + _mustSubtractReservation = false; } /* diff --git a/src/mscorlib/src/System/Runtime/Reliability/CriticalFinalizerObject.cs b/src/mscorlib/src/System/Runtime/Reliability/CriticalFinalizerObject.cs index 6d6d6f3..cbb9562 100644 --- a/src/mscorlib/src/System/Runtime/Reliability/CriticalFinalizerObject.cs +++ b/src/mscorlib/src/System/Runtime/Reliability/CriticalFinalizerObject.cs @@ -11,7 +11,6 @@ ** (i.e. the finalizer is guaranteed to run, won't be aborted by the host and is ** run after the finalizers of other objects collected at the same time). ** -** You must possess UnmanagedCode permission in order to derive from this class. ** ** ===========================================================*/ diff --git a/src/mscorlib/src/System/Threading/ThreadPool.cs b/src/mscorlib/src/System/Threading/ThreadPool.cs index 369bcb0..e457f15 100644 --- a/src/mscorlib/src/System/Threading/ThreadPool.cs +++ b/src/mscorlib/src/System/Threading/ThreadPool.cs @@ -790,16 +790,11 @@ namespace System.Threading { // needed for DangerousAddRef RuntimeHelpers.PrepareConstrainedRegions(); - try + + m_internalWaitObject = waitObject; + if (waitObject != null) { - } - finally - { - m_internalWaitObject = waitObject; - if (waitObject != null) - { - m_internalWaitObject.SafeWaitHandle.DangerousAddRef(ref bReleaseNeeded); - } + m_internalWaitObject.SafeWaitHandle.DangerousAddRef(ref bReleaseNeeded); } } @@ -810,47 +805,43 @@ namespace System.Threading bool result = false; // needed for DangerousRelease RuntimeHelpers.PrepareConstrainedRegions(); - try - { - } - finally + + // lock(this) cannot be used reliably in Cer since thin lock could be + // promoted to syncblock and that is not a guaranteed operation + bool bLockTaken = false; + do { - // lock(this) cannot be used reliably in Cer since thin lock could be - // promoted to syncblock and that is not a guaranteed operation - bool bLockTaken = false; - do + if (Interlocked.CompareExchange(ref m_lock, 1, 0) == 0) { - if (Interlocked.CompareExchange(ref m_lock, 1, 0) == 0) + bLockTaken = true; + try { - bLockTaken = true; - try + if (ValidHandle()) { - if (ValidHandle()) + result = UnregisterWaitNative(GetHandle(), waitObject == null ? null : waitObject.SafeWaitHandle); + if (result == true) { - result = UnregisterWaitNative(GetHandle(), waitObject == null ? null : waitObject.SafeWaitHandle); - if (result == true) + if (bReleaseNeeded) { - if (bReleaseNeeded) - { - m_internalWaitObject.SafeWaitHandle.DangerousRelease(); - bReleaseNeeded = false; - } - // if result not true don't release/suppress here so finalizer can make another attempt - SetHandle(InvalidHandle); - m_internalWaitObject = null; - GC.SuppressFinalize(this); + m_internalWaitObject.SafeWaitHandle.DangerousRelease(); + bReleaseNeeded = false; } + // if result not true don't release/suppress here so finalizer can make another attempt + SetHandle(InvalidHandle); + m_internalWaitObject = null; + GC.SuppressFinalize(this); } } - finally - { - m_lock = 0; - } } - Thread.SpinWait(1); // yield to processor + finally + { + m_lock = 0; + } } - while (!bLockTaken); + Thread.SpinWait(1); // yield to processor } + while (!bLockTaken); + return result; } -- 2.7.4