diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-06-01 18:33:17 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-06-01 18:33:17 -0500 |
| commit | 7239327610f020dcb9028a845709c79a5567869d (patch) | |
| tree | e7b87b066e68f6959fac08025a3ff25e14305162 /src | |
| parent | 18ecfac2d8dc2b5c768cc7cc2dc61fa23259417c (diff) | |
| download | slskd-7239327610f020dcb9028a845709c79a5567869d.tar.gz | |
gracefully handle nulls
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/FileSafety.cs | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/slskd/Common/FileSafety.cs b/src/slskd/Common/FileSafety.cs index 650c082b..9819209f 100644 --- a/src/slskd/Common/FileSafety.cs +++ b/src/slskd/Common/FileSafety.cs @@ -323,6 +323,11 @@ public static class FileSafety /// <returns>The converted path.</returns> public static string LocalizePath(string path, OSPlatform? os = null) { + if (path is null) + { + return null; + } + os ??= Compute.OSPlatform(); var sep = os.Value == OSPlatform.Windows ? '\\' : '/'; @@ -340,6 +345,11 @@ public static class FileSafety /// <returns>The sanitized filename.</returns> public static string SanitizeFilename(string filename, char replacement = '_', OSPlatform? os = null) { + if (filename is null) + { + return null; + } + if (replacement is '/' or '\\') { throw new ArgumentException($"The provided replacement character '{replacement}' is invalid in filenames"); @@ -412,6 +422,11 @@ public static class FileSafety /// <returns>The sanitized path.</returns> public static string SanitizePath(string path, char replacement = '_', bool retainRoot = false, OSPlatform? os = null) { + if (path is null) + { + return null; + } + os ??= Compute.OSPlatform(); var sep = os.Value == OSPlatform.Windows ? '\\' : '/'; @@ -421,7 +436,7 @@ public static class FileSafety if (!retainRoot) { // strip C:\ or //server, if present (regardless of slash variant, etc) - path = StripPathRoot(path); + path = StripPathRoot(path, os); } // for each segment, drop nulls (created by double slashes), sanitize, and replace traversal strings @@ -443,6 +458,11 @@ public static class FileSafety /// <returns>The path with the root stripped, if one was present.</returns> public static string StripPathRoot(string path, OSPlatform? os = null) { + if (path is null) + { + return null; + } + // flip slashes the correct way path = LocalizePath(path, os); |
