Skip to content

Commit 23919a4

Browse files
Enabling CA1416: Validate platform compatibility (#41760)
* Enabling CA1416 * Resolving PR feedback * Updating the .NET analyzers to 5.0.0-rc2.20458.2 * Resolving compilation errors on full framework * Fixing two more tfm issues with the platform compat checks * Adjusting various platform compat checks to work with netstandard and netfx
1 parent 2fc7e69 commit 23919a4

17 files changed

Lines changed: 57 additions & 5 deletions

File tree

eng/Analyzers.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77
<ItemGroup Condition="'$(EnableAnalyzers)' == 'true'">
88
<PackageReference Include="Microsoft.DotNet.CodeAnalysis" Version="$(MicrosoftDotNetCodeAnalysisVersion)" PrivateAssets="all" IsImplicitlyDefined="true" />
9-
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.0-rc1.20413.9" PrivateAssets="all" />
9+
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.0-rc2.20458.2" PrivateAssets="all" />
1010
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.164" PrivateAssets="all" />
1111
</ItemGroup>
1212
</Project>

eng/CodeAnalysis.ruleset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
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 -->
67+
<Rule Id="CA1416" Action="Warning" /> <!-- Validate platform compatibility -->
6868
<Rule Id="CA1417" Action="Warning" /> <!-- Do not use 'OutAttribute' on string parameters for P/Invokes -->
6969
<Rule Id="CA1501" Action="None" /> <!-- Avoid excessive inheritance -->
7070
<Rule Id="CA1502" Action="None" /> <!-- Avoid excessive complexity -->

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

Lines changed: 8 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,10 @@ internal static ErrorCode NCryptGetIntProperty(SafeNCryptHandle hObject, string
5356
{
5457
fixed (int* pResult = &result)
5558
{
59+
#if NETSTANDARD || NETCOREAPP
60+
Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
61+
#endif
62+
5663
errorCode = Interop.NCrypt.NCryptGetProperty(
5764
hObject,
5865
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7+
using System.Runtime.InteropServices;
78
using System.Text;
89

910
namespace Microsoft.Extensions.Logging.EventLog
@@ -166,6 +167,10 @@ private void WriteMessage(string message, EventLogEntryType eventLogEntryType, i
166167

167168
private EventLogEntryType GetEventLogEntryType(LogLevel level)
168169
{
170+
#if NETSTANDARD
171+
Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
172+
#endif
173+
169174
switch (level)
170175
{
171176
case LogLevel.Information:

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

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

44
using System;
5+
using System.Diagnostics;
6+
using System.Runtime.InteropServices;
57
using Microsoft.Extensions.Options;
68

79
namespace Microsoft.Extensions.Logging.EventLog
@@ -53,6 +55,9 @@ public void Dispose()
5355
{
5456
if (_settings.EventLog is WindowsEventLog windowsEventLog)
5557
{
58+
#if NETSTANDARD
59+
Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
60+
#endif
5661
windowsEventLog.DiagnosticsEventLog.Dispose();
5762
}
5863
}

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

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

44
using System;
5+
using System.Diagnostics;
6+
using System.Runtime.InteropServices;
57

68
namespace Microsoft.Extensions.Logging.EventLog
79
{
@@ -53,6 +55,9 @@ private IEventLog CreateDefaultEventLog()
5355
defaultEventId = 1000;
5456
}
5557

58+
#if NETSTANDARD
59+
Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
60+
#endif
5661
return new WindowsEventLog(logName, machineName, sourceName) { DefaultEventId = defaultEventId };
5762
}
5863
}

src/libraries/Microsoft.Extensions.Logging.EventLog/src/Microsoft.Extensions.Logging.EventLog.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
55
<EnableDefaultItems>true</EnableDefaultItems>
6+
<IncludePlatformAttributes>true</IncludePlatformAttributes>
67
</PropertyGroup>
78

89
<ItemGroup>
@@ -18,7 +19,7 @@
1819
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Logging.Abstractions\src\Microsoft.Extensions.Logging.Abstractions.csproj" />
1920
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Options\src\Microsoft.Extensions.Options.csproj" />
2021
</ItemGroup>
21-
22+
2223
<ItemGroup Condition="!$(TargetFramework.StartsWith('net4'))">
2324
<ProjectReference Include="$(LibrariesProjectRoot)System.Diagnostics.EventLog\src\System.Diagnostics.EventLog.csproj" />
2425
</ItemGroup>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
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+
[SupportedOSPlatform("windows")]
1012
internal class WindowsEventLog : IEventLog
1113
{
1214
// https://msdn.microsoft.com/EN-US/library/windows/desktop/aa363679.aspx

0 commit comments

Comments
 (0)