From c1307624d9d99db105ed7b1221c221e444ac24a5 Mon Sep 17 00:00:00 2001 From: Notselwyn <68616630+Notselwyn@users.noreply.github.com> Date: Tue, 18 Jul 2023 15:02:02 +0200 Subject: [PATCH] chore: added function blocks and fixed if statement --- .../Registry/Registry.cs | 19 ++++++++++--------- .../VirtualMachineDetector.cs | 12 ++++++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/SafeExamBrowser.SystemComponents/Registry/Registry.cs b/SafeExamBrowser.SystemComponents/Registry/Registry.cs index cca618db..9e30aaa5 100644 --- a/SafeExamBrowser.SystemComponents/Registry/Registry.cs +++ b/SafeExamBrowser.SystemComponents/Registry/Registry.cs @@ -94,7 +94,9 @@ namespace SafeExamBrowser.SystemComponents.Registry names = default; if (!TryOpenKey(keyName, out var key)) + { return false; + } var success = true; using (key) @@ -117,9 +119,11 @@ namespace SafeExamBrowser.SystemComponents.Registry public bool TryGetSubKeys(string keyName, out IEnumerable subKeys) { subKeys = default; - + if (!TryOpenKey(keyName, out var key)) + { return false; + } var success = true; using (key) @@ -234,15 +238,12 @@ namespace SafeExamBrowser.SystemComponents.Registry try { - if (!TryGetBaseKeyFromKeyName(keyName, out var baseKey, out var subKey)) - return false; - - key = baseKey.OpenSubKey(subKey); - if (key == null) + logger.Info($"default(RegistryKey) == null: {key == null}"); + if (TryGetBaseKeyFromKeyName(keyName, out var baseKey, out var subKey)) { - key = default; - return false; + key = baseKey.OpenSubKey(subKey); } + } catch (Exception e) { @@ -250,7 +251,7 @@ namespace SafeExamBrowser.SystemComponents.Registry return false; } - return true; + return key != default; } } } diff --git a/SafeExamBrowser.SystemComponents/VirtualMachineDetector.cs b/SafeExamBrowser.SystemComponents/VirtualMachineDetector.cs index fc80450a..cab97671 100644 --- a/SafeExamBrowser.SystemComponents/VirtualMachineDetector.cs +++ b/SafeExamBrowser.SystemComponents/VirtualMachineDetector.cs @@ -130,7 +130,9 @@ namespace SafeExamBrowser.SystemComponents */ const string hwConfigParentKey = "HKEY_LOCAL_MACHINE\\SYSTEM\\HardwareConfig"; if (!registry.TryGetSubKeys(hwConfigParentKey, out var hardwareConfigSubkeys)) + { return false; + } foreach (string configId in hardwareConfigSubkeys) { @@ -143,7 +145,9 @@ namespace SafeExamBrowser.SystemComponents didReadKeys &= registry.TryRead(hwConfigKey, "SystemManufacturer", out var systemManufacturer); didReadKeys &= registry.TryRead(hwConfigKey, "SystemProductName", out var systemProductName); if (!didReadKeys) + { continue; + } // reconstruct the systemInfo.biosInfo string var biosInfo = $"{(string) biosVendor} {(string) biosVersion}"; @@ -153,13 +157,17 @@ namespace SafeExamBrowser.SystemComponents // check even more hardware information var computerIdsKey = $"{hwConfigKey}\\ComputerIds"; if (!registry.TryGetNames(computerIdsKey, out var computerIdNames)) + { continue; + } foreach (var computerIdName in computerIdNames) { // collect computer hardware summary (e.g. manufacturer&version&sku&...) if (!registry.TryRead(computerIdsKey, computerIdName, out var computerSummary)) + { continue; + } isVirtualMachine |= IsVirtualSystemInfo((string) computerSummary, (string) systemManufacturer, (string) systemProductName); } @@ -192,12 +200,16 @@ namespace SafeExamBrowser.SystemComponents didReadKeys &= registry.TryRead(cacheIdKey, "DeviceName", out var cacheDeviceName); if (!didReadKeys || deviceName.ToLower() != ((string) cacheDeviceName).ToLower()) + { continue; + } didReadKeys &= registry.TryRead(cacheIdKey, "DeviceMake", out var cacheDeviceManufacturer); didReadKeys &= registry.TryRead(cacheIdKey, "DeviceModel", out var cacheDeviceModel); if (!didReadKeys) + { continue; + } isVirtualMachine |= IsVirtualSystemInfo("", (string) cacheDeviceManufacturer, (string) cacheDeviceModel); }