From 8ab4def93a37b7a565534aab668216407618cc57 Mon Sep 17 00:00:00 2001 From: Michael Spiegel <70846025+Smightym8@users.noreply.github.com> Date: Wed, 31 Jul 2024 17:30:30 +0200 Subject: [PATCH] Fix NullReferenceException on linux systems where the specified paths don't exist (#1300) * feat(SMBios): Handle null reference exception when accessing Board information Changed the ReadSysFs method in the class SMBios to return an empty string instead of null when unable to find the needed paths on Linux, preventing NullReference Exception when getting the manufacturer and model in the Motherboard class. * fix(LMSensors): Prevent exception in LMSensors constructor Introduced string array subDirectories which is initialized empty. Added checking if the hwmon directory exists otherwise the array subDirectories will stay empty so it does not throw an exception if it can't find the path. * Simplify. --------- Co-authored-by: PhyxionNL <7643972+PhyxionNL@users.noreply.github.com> --- .../Hardware/Motherboard/Lpc/LMSensors.cs | 10 +++++++--- LibreHardwareMonitorLib/Hardware/SMBios.cs | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/LMSensors.cs b/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/LMSensors.cs index 5226924..3f80134 100644 --- a/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/LMSensors.cs +++ b/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/LMSensors.cs @@ -16,17 +16,21 @@ namespace LibreHardwareMonitor.Hardware.Motherboard.Lpc; internal class LMSensors { - private readonly List _superIOs = new(); + private const string HwMonPath = "/sys/class/hwmon/"; + private readonly List _superIOs = []; public LMSensors() { - foreach (string basePath in Directory.GetDirectories("/sys/class/hwmon/")) + if (!Directory.Exists(HwMonPath)) + return; + + foreach (string basePath in Directory.GetDirectories(HwMonPath)) { foreach (string devicePath in new[] { "/device", string.Empty }) { string path = basePath + devicePath; - string name = null; + try { using StreamReader reader = new(path + "/name"); diff --git a/LibreHardwareMonitorLib/Hardware/SMBios.cs b/LibreHardwareMonitorLib/Hardware/SMBios.cs index 71a7a0a..0fe5d64 100644 --- a/LibreHardwareMonitorLib/Hardware/SMBios.cs +++ b/LibreHardwareMonitorLib/Hardware/SMBios.cs @@ -1324,11 +1324,11 @@ public class SMBios return reader.ReadLine(); } - return null; + return string.Empty; } catch { - return null; + return string.Empty; } }