Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warnings revealed after enabling analyzer for projects not having access to RuntimeInformation, OSPlatofrm types #42957

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ unsafe void IDispatch.Invoke(
// convert result to VARIANT
if (pVarResult != IntPtr.Zero)
{
#pragma warning disable CA1416 // Validate platform compatibility, cannot use any of guard methods
Marshal.GetNativeVariantForObject(result, pVarResult);
#pragma warning restore CA1416
}

// Now we need to marshal all the byrefs back
Expand All @@ -237,7 +239,9 @@ CustomQueryInterfaceResult ICustomQueryInterface.GetInterface(ref Guid iid, out
ppv = IntPtr.Zero;
if (iid == _iidSourceItf || iid == typeof(IDispatch).GUID)
{
#pragma warning disable CA1416 // Validate platform compatibility, cannot use any of guard methods
ppv = Marshal.GetComInterfaceForObject(this, typeof(IDispatch), CustomQueryInterfaceMode.Ignore);
#pragma warning restore CA1416
return CustomQueryInterfaceResult.Handled;
}

Expand Down Expand Up @@ -267,7 +271,9 @@ private void Unadvise()
try
{
_connectionPoint.Unadvise(_cookie);
#pragma warning disable CA1416 // Validate platform compatibility, cannot use any of guard methods
Marshal.ReleaseComObject(_connectionPoint);
#pragma warning restore CA1416
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public unsafe void CopyFromIndirect(object value)
}
return;
}

#pragma warning disable CA1416 // Validate platform compatibility, cannot use any of guard methods
if ((vt & VarEnum.VT_ARRAY) != 0)
{
Variant vArray;
Expand Down Expand Up @@ -224,6 +224,7 @@ public unsafe void CopyFromIndirect(object value)
default:
throw new ArgumentException();
}
#pragma warning restore CA1416
}

/// <summary>
Expand Down Expand Up @@ -269,7 +270,9 @@ public unsafe void CopyFromIndirect(object value)
{
fixed (void* pThis = &this)
{
#pragma warning disable CA1416 // Validate platform compatibility, cannot use any of guard methods
return Marshal.GetObjectForNativeVariant((System.IntPtr)pThis);
#pragma warning restore CA1416
}
}
}
Expand Down Expand Up @@ -645,6 +648,7 @@ public string AsBstr

// VT_UNKNOWN

#pragma warning disable CA1416 // Validate platform compatibility, cannot use any of guard methods
public object? AsUnknown
{
get
Expand Down Expand Up @@ -698,6 +702,7 @@ public object? AsDispatch
}
}
}
#pragma warning restore CA1416

public IntPtr AsByRefVariant
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ private ComEventSinksContainer()

public static ComEventSinksContainer FromRuntimeCallableWrapper(object rcw, bool createIfNotFound)
{
#pragma warning disable CA1416 // Validate platform compatibility, cannot use any of guard methods
object data = Marshal.GetComObjectData(rcw, s_comObjectEventSinksKey);
if (data != null || createIfNotFound == false)
{
Expand All @@ -41,6 +42,7 @@ public static ComEventSinksContainer FromRuntimeCallableWrapper(object rcw, bool
{
throw Error.SetComObjectDataFailed();
}
#pragma warning restore CA1416

return comEventSinks;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static ComObject ObjectToComObject(object rcw)
{
Debug.Assert(ComBinder.IsComObject(rcw));

#pragma warning disable CA1416 // Validate platform compatibility, cannot use any of guard methods
object data = Marshal.GetComObjectData(rcw, s_comObjectInfoKey);
if (data != null)
{
Expand All @@ -53,6 +54,7 @@ public static ComObject ObjectToComObject(object rcw)
{
throw Error.SetComObjectDataFailed();
}
#pragma warning restore CA1416

return comObjectInfo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ internal static ComTypes.ITypeInfo GetITypeInfoFromIDispatch(IDispatch dispatch)
Marshal.ThrowExceptionForHR(ComHresults.E_FAIL);
}

#pragma warning disable CA1416 // Validate platform compatibility, cannot use any of guard methods
ComTypes.ITypeInfo typeInfo = null;
try
{
Expand All @@ -155,6 +156,7 @@ internal static ComTypes.ITypeInfo GetITypeInfoFromIDispatch(IDispatch dispatch)
{
Marshal.Release(typeInfoPtr);
}
#pragma warning restore CA1416

return typeInfo;
}
Expand Down Expand Up @@ -236,6 +238,7 @@ internal static Variant GetVariantForObject(object obj)
return variant;
}

#pragma warning disable CA1416 // Validate platform compatibility, cannot use any of guard methods
internal static void InitVariantForObject(object obj, ref Variant variant)
{
Debug.Assert(obj != null);
Expand All @@ -258,6 +261,7 @@ public static object GetObjectForVariant(Variant variant)
IntPtr ptr = UnsafeMethods.ConvertVariantByrefToPtr(ref variant);
return Marshal.GetObjectForNativeVariant(ptr);
}
#pragma warning restore CA1416

// This method is intended for use through reflection and should only be used directly by IUnknownReleaseNotZero
public static unsafe int IUnknownRelease(IntPtr interfacePointer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ internal class ComTypeClassDesc : ComTypeDesc, IDynamicMetaObjectProvider

public object CreateInstance()
{
#pragma warning disable CA1416 // Validate platform compatibility, cannot use any of guard methods
if (_typeObj == null)
{
_typeObj = Type.GetTypeFromCLSID(Guid);
}
return Activator.CreateInstance(Type.GetTypeFromCLSID(Guid));
#pragma warning restore CA1416
}

internal ComTypeClassDesc(ComTypes.ITypeInfo typeInfo, ComTypeLibDesc typeLibDesc) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ internal ComTypeEnumDesc(ComTypes.ITypeInfo typeInfo, ComTypeLibDesc typeLibDesc

if (varDesc.varkind == ComTypes.VARKIND.VAR_CONST)
{
#pragma warning disable CA1416 // Validate platform compatibility, cannot use any of guard methods
memberValues[i] = Marshal.GetObjectForNativeVariant(varDesc.desc.lpvarValue);
#pragma warning restore CA1416
}
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ internal override Expression Marshal(Expression parameter)
// parameter.WrappedObject
if (_isWrapper)
{
#pragma warning disable CA1416 // Validate platform compatibility, cannot use any of guard methods
parameter = Expression.Property(
Helpers.Convert(parameter, typeof(DispatchWrapper)),
typeof(DispatchWrapper).GetProperty(nameof(DispatchWrapper.WrappedObject))
);
#pragma warning restore CA1416
}

return Helpers.Convert(parameter, typeof(object));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ private static ComTypes.ITypeInfo GetCoClassTypeInfo(object rcw, ComTypes.ITypeI
if (rcw is IProvideClassInfo provideClassInfo)
{
IntPtr typeInfoPtr = IntPtr.Zero;
#pragma warning disable CA1416 // Validate platform compatibility, cannot use any of guard methods
try
{
provideClassInfo.GetClassInfo(out typeInfoPtr);
Expand All @@ -492,6 +493,7 @@ private static ComTypes.ITypeInfo GetCoClassTypeInfo(object rcw, ComTypes.ITypeI
Marshal.Release(typeInfoPtr);
}
}
#pragma warning restore CA1416
}

// retrieving class information through IPCI has failed -
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ public object AsVariant
{
get
{
#pragma warning disable CA1416 // Validate platform compatibility, cannot use any of guard methods
return Marshal.GetObjectForNativeVariant(UnsafeMethods.ConvertVariantByrefToPtr(ref this));
#pragma warning restore CA1416
}

set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ private Dictionary<int, string> GetStringTable(bool isHelp)
Dictionary<int, string> stringTable;
RegistryKey libraryKey;

Debug.Assert(OperatingSystem.IsWindows());
libraryKey = Registry.PerformanceData;

try
Expand Down Expand Up @@ -208,6 +209,7 @@ internal PerformanceMonitor(string machineName)
[MemberNotNull(nameof(_perfDataKey))]
private void Init()
{
Debug.Assert(OperatingSystem.IsWindows());
if (ProcessManager.IsRemoteMachine(_machineName))
{
_perfDataKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.PerformanceData, _machineName);
Expand Down Expand Up @@ -236,6 +238,7 @@ private void Init()
byte[]? data = null;
int error = 0;

Debug.Assert(OperatingSystem.IsWindows());
while (waitRetries > 0)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ private bool StartCore(ProcessStartInfo startInfo)

private unsafe bool StartWithShellExecuteEx(ProcessStartInfo startInfo)
{
Debug.Assert(OperatingSystem.IsWindows());
if (!string.IsNullOrEmpty(startInfo.UserName) || startInfo.Password != null)
throw new InvalidOperationException(SR.CantStartAsUser);

Expand Down Expand Up @@ -166,6 +167,7 @@ public bool ShellExecuteOnSTAThread()
{
ThreadStart threadStart = new ThreadStart(ShellExecuteFunction);
Thread executionThread = new Thread(threadStart) { IsBackground = true };
Debug.Assert(OperatingSystem.IsWindows());
executionThread.SetApartmentState(ApartmentState.STA);
executionThread.Start();
executionThread.Join();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ private unsafe bool StartWithCreateProcess(ProcessStartInfo startInfo)

if (startInfo.UserName.Length != 0)
{
Debug.Assert(OperatingSystem.IsWindows());
if (startInfo.Password != null && startInfo.PasswordInClearText != null)
{
throw new ArgumentException(SR.CantSetDuplicatePassword);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public string[] Verbs
if (string.IsNullOrEmpty(extension))
return Array.Empty<string>();

Debug.Assert(OperatingSystem.IsWindows());
using (RegistryKey? key = Registry.ClassesRoot.OpenSubKey(extension))
{
if (key == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,9 @@ public partial class PermissiveModifyControl : System.DirectoryServices.Protocol
public partial class QuotaControl : System.DirectoryServices.Protocols.DirectoryControl
{
public QuotaControl() : base (default(string), default(byte[]), default(bool), default(bool)) { }
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
public QuotaControl(System.Security.Principal.SecurityIdentifier querySid) : base (default(string), default(byte[]), default(bool), default(bool)) { }
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
public System.Security.Principal.SecurityIdentifier QuerySid { get { throw null; } set { } }
public override byte[] GetValue() { throw null; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Runtime.Versioning;

namespace System.DirectoryServices.Protocols
{
Expand Down Expand Up @@ -1026,11 +1027,13 @@ public class QuotaControl : DirectoryControl

public QuotaControl() : base("1.2.840.113556.1.4.1852", null, true, true) { }

[SupportedOSPlatform("windows")]
public QuotaControl(SecurityIdentifier querySid) : this()
{
QuerySid = querySid;
}

[SupportedOSPlatform("windows")]
public SecurityIdentifier QuerySid
{
get => _sid == null ? null : new SecurityIdentifier(_sid, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private void StartRaisingEvents()
int session = Interlocked.Increment(ref _currentSession);
byte[] buffer = AllocateBuffer();

Debug.Assert(OperatingSystem.IsWindows());
// Store all state, including a preallocated overlapped, into the state object that'll be
// passed from iteration to iteration during the lifetime of the operation. The buffer will be pinned
// from now until the end of the operation.
Expand All @@ -59,6 +60,7 @@ private void StartRaisingEvents()
{
state.PreAllocatedOverlapped = new PreAllocatedOverlapped((errorCode, numBytes, overlappedPointer) =>
{
Debug.Assert(OperatingSystem.IsWindows());
AsyncReadState state = (AsyncReadState)ThreadPoolBoundHandle.GetNativeOverlappedState(overlappedPointer)!;
state.ThreadPoolBinding.FreeNativeOverlapped(overlappedPointer);
if (state.WeakWatcher.TryGetTarget(out FileSystemWatcher? watcher))
Expand Down Expand Up @@ -153,6 +155,7 @@ private unsafe void Monitor(AsyncReadState state)
if (!_enabled || IsHandleInvalid(state.DirectoryHandle))
return;

Debug.Assert(OperatingSystem.IsWindows());
// Get the overlapped pointer to use for this iteration.
overlappedPointer = state.ThreadPoolBinding.AllocateNativeOverlapped(state.PreAllocatedOverlapped);
continueExecuting = Interop.Kernel32.ReadDirectoryChangesW(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Security.AccessControl;
using System.Security.Principal;

Expand Down Expand Up @@ -35,6 +36,7 @@ internal static void CreateDirectory(string path, IsolatedStorageScope scope)
// (But applies CREATOR OWNER as expected for items and subdirectories.) Setting up front when creating the directory
// doesn't exhibit this behavior, but as we can't currently do that we'll take the rough equivalent for now.

Debug.Assert(OperatingSystem.IsWindows());
DirectorySecurity security = new DirectorySecurity();

// Don't inherit the existing rules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,27 @@ public enum PipeAccessRights
FullControl = 2032031,
AccessSystemSecurity = 16777216,
}
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
public sealed partial class PipeAccessRule : System.Security.AccessControl.AccessRule
{
public PipeAccessRule(System.Security.Principal.IdentityReference identity, System.IO.Pipes.PipeAccessRights rights, System.Security.AccessControl.AccessControlType type) : base (default(System.Security.Principal.IdentityReference), default(int), default(bool), default(System.Security.AccessControl.InheritanceFlags), default(System.Security.AccessControl.PropagationFlags), default(System.Security.AccessControl.AccessControlType)) { }
public PipeAccessRule(string identity, System.IO.Pipes.PipeAccessRights rights, System.Security.AccessControl.AccessControlType type) : base (default(System.Security.Principal.IdentityReference), default(int), default(bool), default(System.Security.AccessControl.InheritanceFlags), default(System.Security.AccessControl.PropagationFlags), default(System.Security.AccessControl.AccessControlType)) { }
public System.IO.Pipes.PipeAccessRights PipeAccessRights { get { throw null; } }
}
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
public sealed partial class PipeAuditRule : System.Security.AccessControl.AuditRule
{
public PipeAuditRule(System.Security.Principal.IdentityReference identity, System.IO.Pipes.PipeAccessRights rights, System.Security.AccessControl.AuditFlags flags) : base (default(System.Security.Principal.IdentityReference), default(int), default(bool), default(System.Security.AccessControl.InheritanceFlags), default(System.Security.AccessControl.PropagationFlags), default(System.Security.AccessControl.AuditFlags)) { }
public PipeAuditRule(string identity, System.IO.Pipes.PipeAccessRights rights, System.Security.AccessControl.AuditFlags flags) : base (default(System.Security.Principal.IdentityReference), default(int), default(bool), default(System.Security.AccessControl.InheritanceFlags), default(System.Security.AccessControl.PropagationFlags), default(System.Security.AccessControl.AuditFlags)) { }
public System.IO.Pipes.PipeAccessRights PipeAccessRights { get { throw null; } }
}
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
public static partial class PipesAclExtensions
{
public static System.IO.Pipes.PipeSecurity GetAccessControl(this System.IO.Pipes.PipeStream stream) { throw null; }
public static void SetAccessControl(this System.IO.Pipes.PipeStream stream, System.IO.Pipes.PipeSecurity pipeSecurity) { }
}
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
public partial class PipeSecurity : System.Security.AccessControl.NativeObjectSecurity
{
public PipeSecurity() : base (default(bool), default(System.Security.AccessControl.ResourceType)) { }
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.

using System.Runtime.ExceptionServices;
using System.Runtime.Versioning;
using System.Threading;

namespace System.IO.Pipes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading;
using Microsoft.Win32.SafeHandles;
using System.Runtime.Versioning;
using System.Diagnostics;

namespace System.IO.Pipes
{
Expand Down Expand Up @@ -135,6 +136,7 @@ private void ValidateRemotePipeUser()
if (!IsCurrentUserOnly)
return;

Debug.Assert(OperatingSystem.IsWindows());
PipeSecurity accessControl = this.GetAccessControl();
IdentityReference? remoteOwnerSid = accessControl.GetOwner(typeof(SecurityIdentifier));
using (WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ private void Create(string pipeName, PipeDirection direction, int maxNumberOfSer
if (IsCurrentUserOnly)
{
Debug.Assert(pipeSecurity == null);
Debug.Assert(OperatingSystem.IsWindows());

using (WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent())
{
Expand Down
Loading