From eb91e30da04a5448d43542d9b135727086f4317f Mon Sep 17 00:00:00 2001 From: William Moy <3392824+wimoy@users.noreply.github.com> Date: Sun, 6 Apr 2025 06:48:46 -0700 Subject: [PATCH] Add missing lock around files variable The files dictionary could be modified and resized by another thread. Without the lock, the read is not synchronized and can cause a KeyNotFoundException. --- .../MockFileSystem.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystem.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystem.cs index 8b1b620a2..3f71206cb 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystem.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystem.cs @@ -156,7 +156,12 @@ private string GetPathWithCorrectDirectoryCapitalization(string fullPath) if (DirectoryExistsWithoutFixingPath(leftHalf)) { - string baseDirectory = files[leftHalf].Path; + string baseDirectory; + lock (files) + { + baseDirectory = files[leftHalf].Path; + } + return baseDirectory + Path.DirectorySeparatorChar + rightHalf; } } @@ -581,4 +586,4 @@ private class FileSystemEntry public string Path { get; set; } public MockFileData Data { get; set; } } -} \ No newline at end of file +}