Skip to content

Commit 3bd3361

Browse files
committed
Enabling CA1416
1 parent 2d463ee commit 3bd3361

File tree

13 files changed

+44
-3
lines changed

13 files changed

+44
-3
lines changed

eng/CodeAnalysis.ruleset

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
<Rule Id="CA1308" Action="None" /> <!-- Normalize strings to uppercase -->
6565
<Rule Id="CA1309" Action="None" /> <!-- Use ordinal stringcomparison -->
6666
<Rule Id="CA1401" Action="Warning" /> <!-- P/Invokes should not be visible -->
67-
<Rule Id="CA1416" Action="None" /> <!-- Validate platform compatibility; https://github.com/dotnet/runtime/issues/41354 -->
6867
<Rule Id="CA1417" Action="Warning" /> <!-- Do not use 'OutAttribute' on string parameters for P/Invokes -->
6968
<Rule Id="CA1501" Action="None" /> <!-- Avoid excessive inheritance -->
7069
<Rule Id="CA1502" Action="None" /> <!-- Avoid excessive complexity -->

src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Properties.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
5+
using System.Diagnostics;
46
using System.Runtime.InteropServices;
7+
using System.Runtime.Versioning;
58
using System.Security.Cryptography;
6-
using System.Diagnostics;
79

810
using Microsoft.Win32.SafeHandles;
911

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

22+
[SupportedOSPlatform("windows")]
2023
internal static ErrorCode NCryptGetByteProperty(SafeNCryptHandle hObject, string pszProperty, ref byte result, CngPropertyOptions options = CngPropertyOptions.None)
2124
{
2225
int cbResult;
@@ -53,6 +56,8 @@ internal static ErrorCode NCryptGetIntProperty(SafeNCryptHandle hObject, string
5356
{
5457
fixed (int* pResult = &result)
5558
{
59+
Debug.Assert(OperatingSystem.IsWindows());
60+
5661
errorCode = Interop.NCrypt.NCryptGetProperty(
5762
hObject,
5863
pszProperty,

src/libraries/Common/src/Interop/Windows/WinSock/SafeNativeOverlapped.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ private void FreeNativeOverlapped()
5757
{
5858
unsafe
5959
{
60+
Debug.Assert(OperatingSystem.IsWindows());
6061
Debug.Assert(_socketHandle != null, "_socketHandle is null.");
6162

6263
ThreadPoolBoundHandle? boundHandle = _socketHandle.IOCPBoundHandle;

src/libraries/Common/src/System/Net/ContextAwareResult.Windows.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
#nullable enable
5+
using System.Diagnostics;
56
using System.Security.Principal;
67

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

1921
private void CleanupInternal()
2022
{
2123
if (_windowsIdentity != null)
2224
{
25+
Debug.Assert(OperatingSystem.IsWindows());
2326
_windowsIdentity.Dispose();
2427
_windowsIdentity = null;
2528
}

src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLogger.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ private void WriteMessage(string message, EventLogEntryType eventLogEntryType, i
164164
}
165165
}
166166

167+
#pragma warning disable CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available
168+
167169
private EventLogEntryType GetEventLogEntryType(LogLevel level)
168170
{
169171
switch (level)
@@ -181,5 +183,7 @@ private EventLogEntryType GetEventLogEntryType(LogLevel level)
181183
return EventLogEntryType.Information;
182184
}
183185
}
186+
187+
#pragma warning restore CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available
184188
}
185189
}

src/libraries/Microsoft.Extensions.Logging.EventLog/src/WindowsEventLog.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
using System;
55
using System.Diagnostics;
66
using System.Security;
7+
using System.Runtime.Versioning;
78

89
namespace Microsoft.Extensions.Logging.EventLog
910
{
11+
#pragma warning disable CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available
12+
1013
internal class WindowsEventLog : IEventLog
1114
{
1215
// https://msdn.microsoft.com/EN-US/library/windows/desktop/aa363679.aspx
@@ -52,4 +55,6 @@ public void WriteEntry(string message, EventLogEntryType type, int eventID, shor
5255
}
5356
}
5457
}
58+
59+
#pragma warning restore CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available
5560
}

src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ public void Save(Stream outputStream)
657657
}
658658
finally
659659
{
660+
Debug.Assert(OperatingSystem.IsWindows());
660661
Marshal.ReleaseComObject(picture);
661662
}
662663
}

src/libraries/System.Drawing.Common/src/System/Drawing/Internal/SystemColorTracker.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ internal static void Add(ISystemColorTracker obj)
3939

4040
if (!addedTracker)
4141
{
42+
Debug.Assert(OperatingSystem.IsWindows());
4243
addedTracker = true;
4344
SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(OnUserPreferenceChanged);
4445
}
@@ -131,6 +132,7 @@ private static void GarbageCollectList()
131132

132133
private static void OnUserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
133134
{
135+
Debug.Assert(OperatingSystem.IsWindows());
134136

135137
// Update pens and brushes
136138
if (e.Category == UserPreferenceCategory.Color)

src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.Win32.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Microsoft.Win32;
55
using System.ComponentModel;
6+
using System.Diagnostics;
67

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

21+
#pragma warning disable CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available
22+
2023
using (RegistryKey serialKey = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM"))
2124
{
2225
if (serialKey != null)
@@ -31,6 +34,8 @@ public static string[] GetPortNames()
3134
}
3235
}
3336

37+
#pragma warning restore CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available
38+
3439
return Array.Empty<string>();
3540
}
3641
}

src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.Windows.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ internal void SetUnmanagedStructures(object? objectsToPin)
5050

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

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

6466
if (asyncResult.InternalPeekCompleted)

0 commit comments

Comments
 (0)