Skip to content

Commit

Permalink
Enabling CA1416
Browse files Browse the repository at this point in the history
  • Loading branch information
tannergooding committed Sep 2, 2020
1 parent 2d463ee commit 3bd3361
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 3 deletions.
1 change: 0 additions & 1 deletion eng/CodeAnalysis.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
<Rule Id="CA1308" Action="None" /> <!-- Normalize strings to uppercase -->
<Rule Id="CA1309" Action="None" /> <!-- Use ordinal stringcomparison -->
<Rule Id="CA1401" Action="Warning" /> <!-- P/Invokes should not be visible -->
<Rule Id="CA1416" Action="None" /> <!-- Validate platform compatibility; https://github.com/dotnet/runtime/issues/41354 -->
<Rule Id="CA1417" Action="Warning" /> <!-- Do not use 'OutAttribute' on string parameters for P/Invokes -->
<Rule Id="CA1501" Action="None" /> <!-- Avoid excessive inheritance -->
<Rule Id="CA1502" Action="None" /> <!-- Avoid excessive complexity -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using System.Diagnostics;

using Microsoft.Win32.SafeHandles;

Expand All @@ -17,6 +19,7 @@ internal static partial class NCrypt
[DllImport(Interop.Libraries.NCrypt, CharSet = CharSet.Unicode)]
internal static extern unsafe ErrorCode NCryptSetProperty(SafeNCryptHandle hObject, string pszProperty, [In] void* pbInput, int cbInput, CngPropertyOptions dwFlags);

[SupportedOSPlatform("windows")]
internal static ErrorCode NCryptGetByteProperty(SafeNCryptHandle hObject, string pszProperty, ref byte result, CngPropertyOptions options = CngPropertyOptions.None)
{
int cbResult;
Expand Down Expand Up @@ -53,6 +56,8 @@ internal static ErrorCode NCryptGetIntProperty(SafeNCryptHandle hObject, string
{
fixed (int* pResult = &result)
{
Debug.Assert(OperatingSystem.IsWindows());

errorCode = Interop.NCrypt.NCryptGetProperty(
hObject,
pszProperty,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ private void FreeNativeOverlapped()
{
unsafe
{
Debug.Assert(OperatingSystem.IsWindows());
Debug.Assert(_socketHandle != null, "_socketHandle is null.");

ThreadPoolBoundHandle? boundHandle = _socketHandle.IOCPBoundHandle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable
using System.Diagnostics;
using System.Security.Principal;

namespace System.Net
Expand All @@ -13,13 +14,15 @@ internal partial class ContextAwareResult
// Security: We need an assert for a call into WindowsIdentity.GetCurrent.
private void SafeCaptureIdentity()
{
Debug.Assert(OperatingSystem.IsWindows());
_windowsIdentity = WindowsIdentity.GetCurrent();
}

private void CleanupInternal()
{
if (_windowsIdentity != null)
{
Debug.Assert(OperatingSystem.IsWindows());
_windowsIdentity.Dispose();
_windowsIdentity = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ private void WriteMessage(string message, EventLogEntryType eventLogEntryType, i
}
}

#pragma warning disable CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available

private EventLogEntryType GetEventLogEntryType(LogLevel level)
{
switch (level)
Expand All @@ -181,5 +183,7 @@ private EventLogEntryType GetEventLogEntryType(LogLevel level)
return EventLogEntryType.Information;
}
}

#pragma warning restore CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
using System;
using System.Diagnostics;
using System.Security;
using System.Runtime.Versioning;

namespace Microsoft.Extensions.Logging.EventLog
{
#pragma warning disable CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available

internal class WindowsEventLog : IEventLog
{
// https://msdn.microsoft.com/EN-US/library/windows/desktop/aa363679.aspx
Expand Down Expand Up @@ -52,4 +55,6 @@ public void WriteEntry(string message, EventLogEntryType type, int eventID, shor
}
}
}

#pragma warning restore CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available
}
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ public void Save(Stream outputStream)
}
finally
{
Debug.Assert(OperatingSystem.IsWindows());
Marshal.ReleaseComObject(picture);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ internal static void Add(ISystemColorTracker obj)

if (!addedTracker)
{
Debug.Assert(OperatingSystem.IsWindows());
addedTracker = true;
SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(OnUserPreferenceChanged);
}
Expand Down Expand Up @@ -131,6 +132,7 @@ private static void GarbageCollectList()

private static void OnUserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
Debug.Assert(OperatingSystem.IsWindows());

// Update pens and brushes
if (e.Category == UserPreferenceCategory.Color)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.Win32;
using System.ComponentModel;
using System.Diagnostics;

namespace System.IO.Ports
{
Expand All @@ -17,6 +18,8 @@ public static string[] GetPortNames()
//
// QueryDosDevice involves finding any ports that map to \Device\Serialx (call with null to get all, then iterate to get the actual device name)

#pragma warning disable CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available

using (RegistryKey serialKey = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM"))
{
if (serialKey != null)
Expand All @@ -31,6 +34,8 @@ public static string[] GetPortNames()
}
}

#pragma warning restore CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available

return Array.Empty<string>();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ internal void SetUnmanagedStructures(object? objectsToPin)

unsafe
{
Debug.Assert(OperatingSystem.IsWindows());
NativeOverlapped* overlapped = boundHandle.AllocateNativeOverlapped(s_ioCallback, this, objectsToPin);
_nativeOverlapped = new SafeNativeOverlapped(s.SafeHandle, overlapped);
}
Expand All @@ -59,6 +60,7 @@ internal void SetUnmanagedStructures(object? objectsToPin)

private static unsafe void CompletionPortCallback(uint errorCode, uint numBytes, NativeOverlapped* nativeOverlapped)
{
Debug.Assert(OperatingSystem.IsWindows());
BaseOverlappedAsyncResult asyncResult = (BaseOverlappedAsyncResult)ThreadPoolBoundHandle.GetNativeOverlappedState(nativeOverlapped)!;

if (asyncResult.InternalPeekCompleted)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ internal ThreadPoolBoundHandle GetOrAllocateThreadPoolBoundHandle(bool trySkipCo

try
{
Debug.Assert(OperatingSystem.IsWindows());
// The handle (this) may have been already released:
// E.g.: The socket has been disposed in the main thread. A completion callback may
// attempt starting another operation.
Expand Down Expand Up @@ -105,6 +106,7 @@ private bool OnHandleClose()
// ThreadPoolBoundHandle allows FreeNativeOverlapped even after it has been disposed.
if (_iocpBoundHandle != null)
{
Debug.Assert(OperatingSystem.IsWindows());
_iocpBoundHandle.Dispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private void InitializeInternals()
bool suppressFlow = !ExecutionContext.IsFlowSuppressed();
try
{
Debug.Assert(OperatingSystem.IsWindows());
if (suppressFlow) ExecutionContext.SuppressFlow();
_preAllocatedOverlapped = new PreAllocatedOverlapped(s_completionPortCallback, _strongThisRef, null);
}
Expand All @@ -75,6 +76,7 @@ private void FreeInternals()

private unsafe NativeOverlapped* AllocateNativeOverlapped()
{
Debug.Assert(OperatingSystem.IsWindows());
Debug.Assert(_operating == InProgress, $"Expected {nameof(_operating)} == {nameof(InProgress)}, got {_operating}");
Debug.Assert(_currentSocket != null, "_currentSocket is null");
Debug.Assert(_currentSocket.SafeHandle != null, "_currentSocket.SafeHandle is null");
Expand All @@ -86,6 +88,7 @@ private void FreeInternals()

private unsafe void FreeNativeOverlapped(NativeOverlapped* overlapped)
{
Debug.Assert(OperatingSystem.IsWindows());
Debug.Assert(overlapped != null, "overlapped is null");
Debug.Assert(_operating == InProgress, $"Expected _operating == InProgress, got {_operating}");
Debug.Assert(_currentSocket != null, "_currentSocket is null");
Expand Down Expand Up @@ -904,6 +907,7 @@ private void FreeOverlapped()
// any pinned buffers.
if (_preAllocatedOverlapped != null)
{
Debug.Assert(OperatingSystem.IsWindows());
_preAllocatedOverlapped.Dispose();
_preAllocatedOverlapped = null!;
}
Expand Down Expand Up @@ -1219,7 +1223,11 @@ private void FinishOperationSendPackets()

private static readonly unsafe IOCompletionCallback s_completionPortCallback = delegate (uint errorCode, uint numBytes, NativeOverlapped* nativeOverlapped)
{
var saeaBox = (StrongBox<SocketAsyncEventArgs>)ThreadPoolBoundHandle.GetNativeOverlappedState(nativeOverlapped)!;
#pragma warning disable CA1416 // https://github.com/dotnet/roslyn-analyzers/issues/4090
Debug.Assert(OperatingSystem.IsWindows());
var saeaBox = (StrongBox<SocketAsyncEventArgs>)(ThreadPoolBoundHandle.GetNativeOverlappedState(nativeOverlapped)!);
#pragma warning restore CA1416 // https://github.com/dotnet/roslyn-analyzers/issues/4090

Debug.Assert(saeaBox.Value != null);
SocketAsyncEventArgs saea = saeaBox.Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ public sealed override byte[] GetSubjectKeyIdentifier(X509Certificate2 certifica

if (keySpec == CryptKeySpec.CERT_NCRYPT_KEY_SPEC)
{
#if NET5_0
Debug.Assert(OperatingSystem.IsWindows());
#endif

using (SafeNCryptKeyHandle keyHandle = new SafeNCryptKeyHandle(handle.DangerousGetHandle(), handle))
{
CngKeyHandleOpenOptions options = CngKeyHandleOpenOptions.None;
Expand Down

0 comments on commit 3bd3361

Please sign in to comment.