From f473ff93301851eea50548426b6e1f1b909444a3 Mon Sep 17 00:00:00 2001 From: Jonathan Pobst Date: Thu, 16 Apr 2020 13:11:46 -0500 Subject: [PATCH] [AndroidSdkWindows] Guard against exception checking registry (#79) Fixes: https://developercommunity.visualstudio.com/content/problem/883179/ilegal-characters-in-path-after-fresh-install.html Somehow, a registry key contains invalid path characters. This causes an `ArgumentException` from `AndroidSdkWindows.CheckRegistryKeyForExecutable()`: System.ArgumentException: Illegal characters in path. at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional) at System.IO.Path.Combine(String path1, String path2) at Xamarin.Android.Tools.AndroidSdkWindows.CheckRegistryKeyForExecutable(UIntPtr key, String subkey, String valueName, Wow64 wow64, String subdir, String exe) at Xamarin.Android.Tools.AndroidSdkWindows.d__37.MoveNext() at System.Linq.Enumerable.d__64`1.MoveNext() at System.Linq.Buffer`1..ctor(IEnumerable`1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source) at Xamarin.Android.Tools.AndroidSdkBase.get_AllAndroidSdks() at Xamarin.Android.Tools.AndroidSdkBase.Initialize(String androidSdkPath, String androidNdkPath, String javaSdkPath) at Xamarin.Android.Tools.AndroidSdkWindows.Initialize(String androidSdkPath, String androidNdkPath, String javaSdkPath) at Xamarin.Android.Tools.AndroidSdkInfo..ctor(Action`2 logger, String androidSdkPath, String androidNdkPath, String javaSdkPath) Handle the `ArgumentException` by wrapping the body of `CheckRegistryKeyForExecutable()` within a `try/catch` block and catch all exceptions. If an exception is thrown, there is no executable of interest. --- .../Sdks/AndroidSdkWindows.cs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Xamarin.Android.Tools.AndroidSdk/Sdks/AndroidSdkWindows.cs b/src/Xamarin.Android.Tools.AndroidSdk/Sdks/AndroidSdkWindows.cs index ce68ee1..792c658 100644 --- a/src/Xamarin.Android.Tools.AndroidSdk/Sdks/AndroidSdkWindows.cs +++ b/src/Xamarin.Android.Tools.AndroidSdk/Sdks/AndroidSdkWindows.cs @@ -303,19 +303,23 @@ public override void SetPreferredAndroidNdkPath (string path) #region Helper Methods private static bool CheckRegistryKeyForExecutable (UIntPtr key, string subkey, string valueName, RegistryEx.Wow64 wow64, string subdir, string exe) { - string key_name = string.Format (@"{0}\{1}\{2}", key == RegistryEx.CurrentUser ? "HKCU" : "HKLM", subkey, valueName); + try { + string key_name = string.Format (@"{0}\{1}\{2}", key == RegistryEx.CurrentUser ? "HKCU" : "HKLM", subkey, valueName); - var path = NullIfEmpty (RegistryEx.GetValueString (key, subkey, valueName, wow64)); + var path = NullIfEmpty (RegistryEx.GetValueString (key, subkey, valueName, wow64)); - if (path == null) { - return false; - } + if (path == null) { + return false; + } - if (!ProcessUtils.FindExecutablesInDirectory (Path.Combine (path, subdir), exe).Any ()) { + if (!ProcessUtils.FindExecutablesInDirectory (Path.Combine (path, subdir), exe).Any ()) { + return false; + } + + return true; + } catch (Exception) { return false; } - - return true; } #endregion