Annotate System.IO.FileSystem.AccessControl for nullable (#31780)
authorbuyaa-n <bunamnan@microsoft.com>
Fri, 7 Feb 2020 18:38:52 +0000 (10:38 -0800)
committerGitHub <noreply@github.com>
Fri, 7 Feb 2020 18:38:52 +0000 (13:38 -0500)
* Annotate System.IO.FileSystem.AccessControl for nullable

* Enabling nullable in ref, removign unneeded bangs

* Remove <Nullable>annotations

* Update formatting

src/libraries/System.IO.FileSystem.AccessControl/ref/System.IO.FileSystem.AccessControl.csproj
src/libraries/System.IO.FileSystem.AccessControl/src/System.IO.FileSystem.AccessControl.csproj
src/libraries/System.IO.FileSystem.AccessControl/src/System/IO/FileSystemAclExtensions.cs
src/libraries/System.IO.FileSystem.AccessControl/src/System/Security/AccessControl/DirectoryObjectSecurity.cs
src/libraries/System.IO.FileSystem.AccessControl/src/System/Security/AccessControl/FileSecurity.cs
src/libraries/System.IO.FileSystem.AccessControl/src/System/Security/AccessControl/FileSystemSecurity.cs

index cf85135..ef697c4 100644 (file)
@@ -3,6 +3,7 @@
     <IsPartialFacadeAssembly Condition="'$(TargetsNetFx)' == 'true'">true</IsPartialFacadeAssembly>
     <TargetFrameworks>netstandard2.0;$(NetFrameworkCurrent);net461</TargetFrameworks>
     <ExcludeCurrentFullFramework>true</ExcludeCurrentFullFramework>
+    <Nullable>enable</Nullable>
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="System.IO.FileSystem.AccessControl.cs" />
index aebcf7f..3f9c4d8 100644 (file)
@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <AllowUnsafeBlocks Condition="'$(TargetsWindows)' == 'true'">true</AllowUnsafeBlocks>
     <IsPartialFacadeAssembly Condition="'$(TargetsNetFx)' == 'true'">true</IsPartialFacadeAssembly>
@@ -6,7 +6,7 @@
     <TargetFrameworks>netstandard2.0;netstandard2.0-Windows_NT;net461-Windows_NT;$(NetCoreAppCurrent)-Windows_NT;$(NetFrameworkCurrent)-Windows_NT</TargetFrameworks>
     <ExcludeCurrentNetCoreApp>true</ExcludeCurrentNetCoreApp>
     <ExcludeCurrentFullFramework>true</ExcludeCurrentFullFramework>
-    <Nullable>annotations</Nullable>
+    <Nullable>enable</Nullable>
   </PropertyGroup>
   <ItemGroup Condition="'$(TargetsNetCoreApp)' != 'true'">
     <Compile Include="$(CommonPath)System\Runtime\InteropServices\SuppressGCTransitionAttribute.internal.cs" />
index e660091..80d3dd7 100644 (file)
@@ -322,7 +322,7 @@ namespace System.IO
                 // probably be consistent w/ every other directory.
                 int errorCode = Marshal.GetLastWin32Error();
 
-                if (errorCode == Interop.Errors.ERROR_PATH_NOT_FOUND && fullPath.Length == Path.GetPathRoot(fullPath).Length)
+                if (errorCode == Interop.Errors.ERROR_PATH_NOT_FOUND && fullPath.Length == Path.GetPathRoot(fullPath)!.Length)
                 {
                     errorCode = Interop.Errors.ERROR_ACCESS_DENIED;
                 }
index 7bfd44f..6ec2ff8 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+using System.Diagnostics;
 using System.Security.Principal;
 
 namespace System.Security.AccessControl
@@ -58,7 +59,7 @@ namespace System.Security.AccessControl
                     throw new ArgumentException(SR.Arg_MustBeIdentityReferenceType, nameof(targetType));
                 }
 
-                CommonAcl acl = null;
+                CommonAcl? acl = null;
 
                 if (access)
                 {
@@ -83,7 +84,7 @@ namespace System.Security.AccessControl
                     return result;
                 }
 
-                IdentityReferenceCollection irTarget = null;
+                IdentityReferenceCollection? irTarget = null;
 
                 if (targetType != typeof(SecurityIdentifier))
                 {
@@ -100,7 +101,7 @@ namespace System.Security.AccessControl
                         // A better way would be to have an internal method that would canonicalize the ACL
                         // and call it once, then use the RawAcl.
                         //
-                        QualifiedAce ace = acl[i] as QualifiedAce;
+                        QualifiedAce? ace = acl[i] as QualifiedAce;
 
                         if (ace == null)
                         {
@@ -150,7 +151,7 @@ namespace System.Security.AccessControl
                     // A better way would be to have an internal method that would canonicalize the ACL
                     // and call it once, then use the RawAcl.
                     //
-                    QualifiedAce ace = acl[i] as CommonAce;
+                    QualifiedAce? ace = acl[i] as CommonAce;
 
                     if (ace == null)
                     {
@@ -189,7 +190,7 @@ namespace System.Security.AccessControl
 
                     if ((includeExplicit && ((ace.AceFlags & AceFlags.Inherited) == 0)) || (includeInherited && ((ace.AceFlags & AceFlags.Inherited) != 0)))
                     {
-                        IdentityReference iref = (targetType == typeof(SecurityIdentifier)) ? ace.SecurityIdentifier : irTarget[i];
+                        IdentityReference iref = (targetType == typeof(SecurityIdentifier)) ? ace.SecurityIdentifier : irTarget![i];
 
                         if (access)
                         {
@@ -204,15 +205,13 @@ namespace System.Security.AccessControl
                                 type = AccessControlType.Deny;
                             }
 
-                            if (ace is ObjectAce)
+                            if (ace is ObjectAce objectAce)
                             {
-                                ObjectAce objectAce = ace as ObjectAce;
-
                                 result.AddRule(AccessRuleFactory(iref, objectAce.AccessMask, objectAce.IsInherited, objectAce.InheritanceFlags, objectAce.PropagationFlags, type, objectAce.ObjectAceType, objectAce.InheritedObjectAceType));
                             }
                             else
                             {
-                                CommonAce commonAce = ace as CommonAce;
+                                CommonAce? commonAce = ace as CommonAce;
 
                                 if (commonAce == null)
                                 {
@@ -224,15 +223,13 @@ namespace System.Security.AccessControl
                         }
                         else
                         {
-                            if (ace is ObjectAce)
+                            if (ace is ObjectAce objectAce)
                             {
-                                ObjectAce objectAce = ace as ObjectAce;
-
                                 result.AddRule(AuditRuleFactory(iref, objectAce.AccessMask, objectAce.IsInherited, objectAce.InheritanceFlags, objectAce.PropagationFlags, objectAce.AuditFlags, objectAce.ObjectAceType, objectAce.InheritedObjectAceType));
                             }
                             else
                             {
-                                CommonAce commonAce = ace as CommonAce;
+                                CommonAce? commonAce = ace as CommonAce;
 
                                 if (commonAce == null)
                                 {
@@ -291,8 +288,9 @@ namespace System.Security.AccessControl
                 }
             }
 
-            SecurityIdentifier sid = rule.IdentityReference.Translate(typeof(SecurityIdentifier)) as SecurityIdentifier;
+            SecurityIdentifier sid = (SecurityIdentifier)rule.IdentityReference.Translate(typeof(SecurityIdentifier));
 
+            Debug.Assert(SecurityDescriptor.DiscretionaryAcl != null);
             if (rule.AccessControlType == AccessControlType.Allow)
             {
                 switch (modification)
@@ -431,8 +429,9 @@ nameof(modification),
                 }
             }
 
-            SecurityIdentifier sid = rule.IdentityReference.Translate(typeof(SecurityIdentifier)) as SecurityIdentifier;
+            SecurityIdentifier sid = (SecurityIdentifier)rule.IdentityReference.Translate(typeof(SecurityIdentifier));
 
+            Debug.Assert(SecurityDescriptor.SystemAcl != null);
             switch (modification)
             {
                 case AccessControlModification.Add:
@@ -504,7 +503,7 @@ nameof(modification),
             //        SR.AccessControl_InvalidAccessRuleType,
             //        "rule");
             //}
-            return ModifyAccess(modification, rule as ObjectAccessRule, out modified);
+            return ModifyAccess(modification, (ObjectAccessRule)rule, out modified);
         }
 
         protected override bool ModifyAudit(AccessControlModification modification, AuditRule rule, out bool modified)
@@ -516,7 +515,7 @@ nameof(modification),
             //        SR.AccessControl_InvalidAuditRuleType,
             //        "rule");
             //}
-            return ModifyAudit(modification, rule as ObjectAuditRule, out modified);
+            return ModifyAudit(modification, (ObjectAuditRule)rule, out modified);
         }
 #endregion
 
index ac531a9..8a4a089 100644 (file)
@@ -19,7 +19,7 @@ namespace System.Security.AccessControl
         {
         }
 
-        internal FileSecurity(SafeFileHandle handle, AccessControlSections includeSections)
+        internal FileSecurity(SafeFileHandle? handle, AccessControlSections includeSections)
             : base(false, handle, includeSections, false)
         {
         }
index e17cbf5..311ded9 100644 (file)
@@ -23,14 +23,14 @@ namespace System.Security.AccessControl
         {
         }
 
-        internal FileSystemSecurity(bool isContainer, SafeFileHandle handle, AccessControlSections includeSections, bool isDirectory)
+        internal FileSystemSecurity(bool isContainer, SafeFileHandle? handle, AccessControlSections includeSections, bool isDirectory)
             : base(isContainer, s_ResourceType, handle, includeSections, _HandleErrorCode, isDirectory)
         {
         }
 
-        private static Exception _HandleErrorCode(int errorCode, string name, SafeHandle handle, object context)
+        private static Exception? _HandleErrorCode(int errorCode, string? name, SafeHandle? handle, object? context)
         {
-            Exception exception = null;
+            Exception? exception = null;
 
             switch (errorCode)
             {
@@ -176,9 +176,7 @@ namespace System.Security.AccessControl
 
             for (int i = 0; i < rules.Count; i++)
             {
-                FileSystemAccessRule fsrule = rules[i] as FileSystemAccessRule;
-
-                if ((fsrule != null) && (fsrule.FileSystemRights == rule.FileSystemRights)
+                if ((rules[i] is FileSystemAccessRule fsrule) && (fsrule.FileSystemRights == rule.FileSystemRights)
                     && (fsrule.IdentityReference == rule.IdentityReference)
                     && (fsrule.AccessControlType == rule.AccessControlType))
                 {
@@ -222,9 +220,7 @@ namespace System.Security.AccessControl
 
             for (int i = 0; i < rules.Count; i++)
             {
-                FileSystemAccessRule fsrule = rules[i] as FileSystemAccessRule;
-
-                if ((fsrule != null) && (fsrule.FileSystemRights == rule.FileSystemRights)
+                if ((rules[i] is FileSystemAccessRule fsrule) && (fsrule.FileSystemRights == rule.FileSystemRights)
                     && (fsrule.IdentityReference == rule.IdentityReference)
                     && (fsrule.AccessControlType == rule.AccessControlType))
                 {