Skip to content

Commit

Permalink
[AndroidSdkWindows] Guard against exception checking registry (#79)
Browse files Browse the repository at this point in the history
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.<GetAllAvailableAndroidSdks>d__37.MoveNext()
	   at System.Linq.Enumerable.<DistinctIterator>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.
  • Loading branch information
jpobst authored Apr 16, 2020
1 parent 36d7fee commit f473ff9
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/Xamarin.Android.Tools.AndroidSdk/Sdks/AndroidSdkWindows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit f473ff9

Please sign in to comment.