Skip to content

Commit

Permalink
Enable CA1419 (SafeHandle public parameterless ctor) (#55460)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Jul 12, 2021
1 parent 375ca44 commit 633e044
Show file tree
Hide file tree
Showing 47 changed files with 94 additions and 25 deletions.
1 change: 1 addition & 0 deletions eng/CodeAnalysis.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Rule Id="CA1416" Action="Warning" /> <!-- Validate platform compatibility -->
<Rule Id="CA1417" Action="Warning" /> <!-- Do not use 'OutAttribute' on string parameters for P/Invokes -->
<Rule Id="CA1418" Action="Warning" /> <!-- Use valid platform string -->
<Rule Id="CA1419" Action="Warning" /> <!-- Provide a public parameterless constructor for concrete types derived from 'System.Runtime.InteropServices.SafeHandle' -->
<Rule Id="CA1501" Action="None" /> <!-- Avoid excessive inheritance -->
<Rule Id="CA1502" Action="None" /> <!-- Avoid excessive complexity -->
<Rule Id="CA1505" Action="None" /> <!-- Avoid unmaintainable code -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ internal sealed class SafeTemporaryKeychainHandle : SafeKeychainHandle
private static readonly Dictionary<IntPtr, SafeTemporaryKeychainHandle> s_lookup =
new Dictionary<IntPtr, SafeTemporaryKeychainHandle>();

internal SafeTemporaryKeychainHandle()
public SafeTemporaryKeychainHandle()
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ internal sealed class SafeLocalFreeChannelBinding : ChannelBinding
{
private int _size;

private SafeLocalFreeChannelBinding() { }
public SafeLocalFreeChannelBinding() { }

public override int Size
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static unsafe int QueryContextAttributes(SafeDeleteContext phContext, Int

internal sealed class SafeFreeContextBuffer_SECURITY : SafeFreeContextBuffer
{
internal SafeFreeContextBuffer_SECURITY() : base() { }
public SafeFreeContextBuffer_SECURITY() : base() { }

protected override bool ReleaseHandle()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ public override bool IsInvalid

internal sealed class SafeSharedAsn1IntegerHandle : SafeInteriorHandle
{
private SafeSharedAsn1IntegerHandle() :
public SafeSharedAsn1IntegerHandle() :
base(IntPtr.Zero, ownsHandle: true)
{
}
}

internal sealed class SafeSharedAsn1OctetStringHandle : SafeInteriorHandle
{
private SafeSharedAsn1OctetStringHandle() :
public SafeSharedAsn1OctetStringHandle() :
base(IntPtr.Zero, ownsHandle: true)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public override bool IsInvalid
}

protected override bool ReleaseHandle() => throw new PlatformNotSupportedException();
private SafeGssNameHandle()

public SafeGssNameHandle()
: base(IntPtr.Zero, true)
{
}
Expand All @@ -27,7 +28,7 @@ private SafeGssNameHandle()
[UnsupportedOSPlatform("tvos")]
internal sealed class SafeGssCredHandle : SafeHandle
{
private SafeGssCredHandle()
public SafeGssCredHandle()
: base(IntPtr.Zero, true)
{
}
Expand All @@ -43,7 +44,7 @@ public override bool IsInvalid
[UnsupportedOSPlatform("tvos")]
internal sealed class SafeGssContextHandle : SafeHandle
{
private SafeGssContextHandle()
public SafeGssContextHandle()
: base(IntPtr.Zero, true)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System;
using System.Runtime.InteropServices;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace Microsoft.Win32.SafeHandles
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal sealed class SecurityContextTokenHandle : CriticalHandleZeroOrMinusOneI
#endif
private int _disposed;

private SecurityContextTokenHandle() : base()
public SecurityContextTokenHandle() : base()
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Text;
using Microsoft.Win32.SafeHandles;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Net.Security
{
internal sealed class SafeDeleteNegoContext : SafeDeleteContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Net.Security
{
internal sealed class SafeDeleteSslContext : SafeDeleteContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Net.Security
{
#if DEBUG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Text;
using Microsoft.Win32.SafeHandles;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Net.Security
{
internal sealed class SafeFreeNegoCredentials : SafeFreeCredentials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Net.Security
{
internal sealed class SafeFreeSslCredentials : SafeFreeCredentials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Data.Odbc
{
internal sealed class OdbcConnectionHandle : OdbcHandle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Runtime.InteropServices;
using System.Text;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Data.Odbc
{
internal abstract class OdbcHandle : SafeHandle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Data.Common;
using System.Runtime.InteropServices;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Data.Odbc
{
internal readonly struct SQLLEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Runtime.InteropServices;
using System.Text;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Data.Odbc
{
internal sealed class CNativeBuffer : System.Data.ProviderBase.DbBuffer
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/System.Data.OleDb/src/OleDbTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Data.OleDb
{
public sealed class OleDbTransaction : DbTransaction
Expand Down
6 changes: 4 additions & 2 deletions src/libraries/System.Data.OleDb/src/OleDbWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Data.OleDb
{
// SafeHandle wrapper around 'DataLinks' object which pools the native OLE DB providers.
Expand Down Expand Up @@ -92,7 +94,7 @@ internal sealed class DataSourceWrapper : WrappedIUnknown
// we expect to store IDBInitialize instance pointer in base.handle

// construct a DataSourceWrapper and used as a ref parameter to GetDataSource
internal DataSourceWrapper() : base()
public DataSourceWrapper() : base()
{
}

Expand Down Expand Up @@ -229,7 +231,7 @@ internal sealed class SessionWrapper : WrappedIUnknown
// since we maintain an AddRef on IDBCreateCommand it is safe to use the delegate without rechecking its function pointer
private UnsafeNativeMethods.IDBCreateCommandCreateCommand? DangerousIDBCreateCommandCreateCommand;

internal SessionWrapper() : base()
public SessionWrapper() : base()
{
}

Expand Down
2 changes: 2 additions & 0 deletions src/libraries/System.Data.OleDb/src/PropertyIDSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Data.ProviderBase;
using System.Runtime.InteropServices;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Data.OleDb
{
internal sealed class PropertyIDSet : DbBuffer
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/System.Data.OleDb/src/PropertyInfoSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Data.OleDb
{
internal sealed class OleDbPropertyInfo
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/System.Data.OleDb/src/RowBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Data.OleDb
{
internal sealed class RowBinding : System.Data.ProviderBase.DbBuffer
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/System.Data.OleDb/src/SafeHandles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Runtime.Versioning;
using static System.Data.Common.UnsafeNativeMethods;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Data.OleDb
{
internal sealed class DualCoTaskMem : SafeHandle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private sealed class PoolWaitHandles : DbBuffer

private readonly int _releaseFlags;

internal PoolWaitHandles() : base(3 * IntPtr.Size)
public PoolWaitHandles() : base(3 * IntPtr.Size)
{
bool mustRelease1 = false, mustRelease2 = false, mustRelease3 = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,9 @@ public override void Dispose()
//
private sealed class SafeMemoryPtr : SafeHandle
{
private SafeMemoryPtr() : base(IntPtr.Zero, true)
{ }
public SafeMemoryPtr() : base(IntPtr.Zero, true)
{
}

internal SafeMemoryPtr(IntPtr handle) : base(IntPtr.Zero, true)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.DirectoryServices.Protocols
{
internal sealed class HGlobalMemHandle : SafeHandleZeroOrMinusOneIsInvalid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ namespace System.Drawing.Drawing2D
{
internal sealed class SafeCustomLineCapHandle : SafeHandle
{
public SafeCustomLineCapHandle() : base(IntPtr.Zero, true)
{
}

// Create a SafeHandle, informing the base class
// that this SafeHandle instance "owns" the handle,
// and therefore SafeHandle should call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
using System.Runtime.InteropServices;
using System.Security.Authentication.ExtendedProtection;
using System.Text;

using SafeWinHttpHandle = Interop.WinHttp.SafeWinHttpHandle;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Net.Http
{
internal sealed class WinHttpChannelBinding : ChannelBinding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Microsoft.Win32.SafeHandles;
using System.Threading;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Net
{
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ internal unsafe sealed class MsQuicApi
// This is workaround for a bug in ILTrimmer.
// Without these DynamicDependency attributes, .ctor() will be removed from the safe handles.
// Remove once fixed: https://github.com/mono/linker/issues/1660
[DynamicDependency(DynamicallyAccessedMemberTypes.NonPublicConstructors, typeof(SafeMsQuicRegistrationHandle))]
[DynamicDependency(DynamicallyAccessedMemberTypes.NonPublicConstructors, typeof(SafeMsQuicConfigurationHandle))]
[DynamicDependency(DynamicallyAccessedMemberTypes.NonPublicConstructors, typeof(SafeMsQuicListenerHandle))]
[DynamicDependency(DynamicallyAccessedMemberTypes.NonPublicConstructors, typeof(SafeMsQuicConnectionHandle))]
[DynamicDependency(DynamicallyAccessedMemberTypes.NonPublicConstructors, typeof(SafeMsQuicStreamHandle))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(SafeMsQuicRegistrationHandle))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(SafeMsQuicConfigurationHandle))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(SafeMsQuicListenerHandle))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(SafeMsQuicConnectionHandle))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(SafeMsQuicStreamHandle))]
private MsQuicApi(NativeApi* vtable)
{
uint status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal sealed class SafeMsQuicConfigurationHandle : SafeHandle

public override bool IsInvalid => handle == IntPtr.Zero;

private SafeMsQuicConfigurationHandle()
public SafeMsQuicConfigurationHandle()
: base(IntPtr.Zero, ownsHandle: true)
{ }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal sealed class SafeMsQuicConnectionHandle : SafeHandle
{
public override bool IsInvalid => handle == IntPtr.Zero;

private SafeMsQuicConnectionHandle()
public SafeMsQuicConnectionHandle()
: base(IntPtr.Zero, ownsHandle: true)
{ }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal sealed class SafeMsQuicListenerHandle : SafeHandle
{
public override bool IsInvalid => handle == IntPtr.Zero;

private SafeMsQuicListenerHandle()
public SafeMsQuicListenerHandle()
: base(IntPtr.Zero, ownsHandle: true)
{ }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal sealed class SafeMsQuicRegistrationHandle : SafeHandle
{
public override bool IsInvalid => handle == IntPtr.Zero;

private SafeMsQuicRegistrationHandle()
public SafeMsQuicRegistrationHandle()
: base(IntPtr.Zero, ownsHandle: true)
{ }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal sealed class SafeMsQuicStreamHandle : SafeHandle
{
public override bool IsInvalid => handle == IntPtr.Zero;

private SafeMsQuicStreamHandle()
public SafeMsQuicStreamHandle()
: base(IntPtr.Zero, ownsHandle: true)
{ }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using PAL_KeyAlgorithm = Interop.AndroidCrypto.PAL_KeyAlgorithm;
using PAL_SSLStreamStatus = Interop.AndroidCrypto.PAL_SSLStreamStatus;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Net
{
internal sealed class SafeDeleteSslContext : SafeDeleteContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Security.Authentication.ExtendedProtection;
using System.Text;

#pragma warning disable CA1419 // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke

namespace System.Net.Security
{
internal sealed class SafeChannelBindingHandle : ChannelBinding
Expand Down
Loading

0 comments on commit 633e044

Please sign in to comment.