From 2b562e8c34a9de41cf6aceaf4d162ef978e6b6ef Mon Sep 17 00:00:00 2001 From: "Dylan R. E. Moonfire" Date: Mon, 6 Dec 2021 22:31:01 -0600 Subject: [PATCH] fix: protect against non-existent directories while finding --- src/MfGames.IO/Extensions/DirectoryInfoExtensions.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/MfGames.IO/Extensions/DirectoryInfoExtensions.cs b/src/MfGames.IO/Extensions/DirectoryInfoExtensions.cs index 67e9fbb..df53189 100644 --- a/src/MfGames.IO/Extensions/DirectoryInfoExtensions.cs +++ b/src/MfGames.IO/Extensions/DirectoryInfoExtensions.cs @@ -21,7 +21,7 @@ namespace MfGames.IO.Extensions // Ignore blanks. if (directory == null) { - return directory; + return null; } // Check the parent first. We don't have to worry about null because @@ -83,14 +83,18 @@ namespace MfGames.IO.Extensions throw new ArgumentNullException(nameof(match)); } - // If the directory is null, just return null. - if (directory == null) + // If the directory is null, just return null. Same with + // non-existing directories. + if (directory is not { Exists: true }) { return null; } // Check this directory for a match, otherwise move up. - if (match(directory)) return directory; + if (match(directory)) + { + return directory; + } directory = directory.Parent; }