Guard against attempting to creating out-of-range spans in macOS FileSystemWatcher...
authorStephen Toub <stoub@microsoft.com>
Mon, 21 Oct 2019 22:13:35 +0000 (18:13 -0400)
committerGitHub <noreply@github.com>
Mon, 21 Oct 2019 22:13:35 +0000 (18:13 -0400)
* Guard against attempting to creating out-of-range spans in macOS FilesystemWatcher

Fixes https://github.com/dotnet/corefx/issues/40054

* Address PR feedback

Commit migrated from https://github.com/dotnet/corefx/commit/4277ccfa457d0dfad3fad00f8adbd27781b3bc57

src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs

index 8c0f2c5..7298e00 100644 (file)
@@ -422,7 +422,7 @@ namespace System.IO
                         // The base FileSystemWatcher does a match check against the relative path before combining with
                         // the root dir; however, null is special cased to signify the root dir, so check if we should use that.
                         ReadOnlySpan<char> relativePath = ReadOnlySpan<char>.Empty;
-                        if (!path.Equals(_fullDirectory, StringComparison.OrdinalIgnoreCase))
+                        if (path.Length > _fullDirectory.Length && path.StartsWith(_fullDirectory, StringComparison.OrdinalIgnoreCase))
                         {
                             // Remove the root directory to get the relative path
                             relativePath = path.Slice(_fullDirectory.Length);
@@ -467,7 +467,12 @@ namespace System.IO
                             {
                                 // Remove the base directory prefix and add the paired event to the list of
                                 // events to skip and notify the user of the rename
-                                ReadOnlySpan<char> newPathRelativeName = events[pairedId].Span.Slice(_fullDirectory.Length);
+                                ReadOnlySpan<char> newPathRelativeName = events[pairedId].Span;
+                                if (newPathRelativeName.Length >= _fullDirectory.Length &&
+                                    newPathRelativeName.StartsWith(_fullDirectory, StringComparison.OrdinalIgnoreCase))
+                                {
+                                    newPathRelativeName = newPathRelativeName.Slice(_fullDirectory.Length);
+                                }
                                 watcher.NotifyRenameEventArgs(WatcherChangeTypes.Renamed, newPathRelativeName, relativePath);
 
                                 // Create a new list, if necessary, and add the event