Nullable Internal.Win32.RegistryKey (#23856)
authorAnirudh Agnihotry <anirudhagnihotry098@gmail.com>
Wed, 10 Apr 2019 19:18:42 +0000 (12:18 -0700)
committerStephen Toub <stoub@microsoft.com>
Wed, 10 Apr 2019 19:18:42 +0000 (15:18 -0400)
* anootated registrykey

* Fixing the build for windows

* Adding bang after the resize argument

src/System.Private.CoreLib/shared/Internal/Win32/RegistryKey.cs
src/System.Private.CoreLib/shared/System/Globalization/HijriCalendar.Win32.cs
src/System.Private.CoreLib/shared/System/Globalization/JapaneseCalendar.Win32.cs

index a98eef9..469d678 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.
 
+#nullable enable
 using System;
 using System.Buffers;
 using System.Collections.Generic;
@@ -28,7 +29,7 @@ namespace Internal.Win32
         private const int MaxKeyLength = 255;
         private const int MaxValueLength = 16383;
 
-        private SafeRegistryHandle _hkey = null;
+        private SafeRegistryHandle _hkey;
 
         private RegistryKey(SafeRegistryHandle hkey)
         {
@@ -75,12 +76,12 @@ namespace Internal.Win32
             return new RegistryKey(new SafeRegistryHandle(hKey, false));
         }
 
-        public RegistryKey OpenSubKey(string name)
+        public RegistryKey? OpenSubKey(string name)
         {
             return OpenSubKey(name, false);
         }
 
-        public RegistryKey OpenSubKey(string name, bool writable)
+        public RegistryKey? OpenSubKey(string name, bool writable)
         {
             // Make sure that the name does not contain double slahes
             Debug.Assert(name.IndexOf("\\\\") == -1);
@@ -162,7 +163,7 @@ namespace Internal.Win32
             // add up quickly- we'll try to keep the memory pressure low and grow the buffer
             // only if needed.
 
-            char[] name = ArrayPool<char>.Shared.Rent(100);
+            char[]? name = ArrayPool<char>.Shared.Rent(100);
 
             try
             {
@@ -214,18 +215,18 @@ namespace Internal.Win32
             return names.ToArray();
         }
 
-        public object GetValue(string name)
+        public object? GetValue(string name)
         {
             return GetValue(name, null);
         }
 
-        public object GetValue(string name, object defaultValue)
+        public object? GetValue(string name, object? defaultValue) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
         {
-            object data = defaultValue;
+            object? data = defaultValue;
             int type = 0;
             int datasize = 0;
 
-            int ret = Interop.Advapi32.RegQueryValueEx(_hkey, name, null, ref type, (byte[])null, ref datasize);
+            int ret = Interop.Advapi32.RegQueryValueEx(_hkey, name, null, ref type, (byte[]?)null, ref datasize);
 
             if (ret != 0)
             {
@@ -369,7 +370,7 @@ namespace Internal.Win32
                         // make sure the string is null terminated before processing the data
                         if (blob.Length > 0 && blob[blob.Length - 1] != (char)0)
                         {
-                            Array.Resize(ref blob, blob.Length + 1);
+                            Array.Resize(ref blob!, blob.Length + 1); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
                         }
 
                         string[] strings = Array.Empty<string>();
@@ -386,7 +387,7 @@ namespace Internal.Win32
                                 nextNull++;
                             }
 
-                            string toAdd = null;
+                            string? toAdd = null;
                             if (nextNull < len)
                             {
                                 Debug.Assert(blob[nextNull] == (char)0, "blob[nextNull] should be 0");
@@ -414,13 +415,13 @@ namespace Internal.Win32
                             {
                                 if (strings.Length == stringsCount)
                                 {
-                                    Array.Resize(ref strings, stringsCount > 0 ? stringsCount * 2 : 4);
+                                    Array.Resize(ref strings!, stringsCount > 0 ? stringsCount * 2 : 4); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
                                 }
-                                strings[stringsCount++] = toAdd;
+                                strings![stringsCount++] = toAdd; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
                             }
                         }
 
-                        Array.Resize(ref strings, stringsCount);
+                        Array.Resize(ref strings!, stringsCount); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
                         data = strings;
                     }
                     break;
@@ -455,7 +456,7 @@ namespace Internal.Win32
             }
         }
 
-        internal void Win32Error(int errorCode, string str)
+        internal void Win32Error(int errorCode, string? str)
         {
             switch (errorCode)
             {
index 02e38e2..1e8f747 100644 (file)
@@ -40,7 +40,7 @@ namespace System.Globalization
         ============================================================================*/
         private static int GetAdvanceHijriDate()
         {
-            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(InternationalRegKey))
+            using (RegistryKey? key = Registry.CurrentUser.OpenSubKey(InternationalRegKey))
             {
                 // Abort if we didn't find anything
                 if (key == null)
@@ -48,7 +48,7 @@ namespace System.Globalization
                     return 0;
                 }
 
-                object value = key.GetValue(HijriAdvanceRegKeyEntry);
+                object? value = key.GetValue(HijriAdvanceRegKeyEntry);
                 if (value == null)
                 {
                     return 0;
index b7cd764..780b953 100644 (file)
@@ -37,7 +37,7 @@ namespace System.Globalization
             try
             {
                 // Need to access registry
-                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(JapaneseErasHive))
+                using (RegistryKey? key = Registry.LocalMachine.OpenSubKey(JapaneseErasHive))
                 {
                     // Abort if we didn't find anything
                     if (key == null) return null;
@@ -52,7 +52,7 @@ namespace System.Globalization
                         for (int i = 0; i < valueNames.Length; i++)
                         {
                             // See if the era is a valid date
-                            EraInfo? era = GetEraFromValue(valueNames[i], key.GetValue(valueNames[i]).ToString());
+                            EraInfo? era = GetEraFromValue(valueNames[i], key.GetValue(valueNames[i])?.ToString());
 
                             // continue if not valid
                             if (era == null) continue;