From 34a972b6a6d9bf777810103e9b252992e2831930 Mon Sep 17 00:00:00 2001 From: Artjom Date: Fri, 26 Jun 2026 08:14:08 +0200 Subject: [PATCH] Guard PluginLoader directory scan against enumeration exceptions --- NanoXLSX.Core/Registry/PluginLoader.cs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/NanoXLSX.Core/Registry/PluginLoader.cs b/NanoXLSX.Core/Registry/PluginLoader.cs index 08b150a4a..9977959d0 100644 --- a/NanoXLSX.Core/Registry/PluginLoader.cs +++ b/NanoXLSX.Core/Registry/PluginLoader.cs @@ -76,9 +76,26 @@ private static void LoadReferencedAssemblies() .Select(a => a.Location); HashSet loadedPaths = new HashSet(allLoadedPaths, StringComparer.InvariantCultureIgnoreCase); - List referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll") - .Where(path => !loadedPaths.Contains(path)) - .ToList(); + // Guard the assembly-directory enumeration. Directory.GetFiles can throw (e.g. IOException + // "The parameter is incorrect" when BaseDirectory contains an entry the native enumerator + // cannot stat — observed when the host process's base directory is the Unity 6000.5.x editor + // install folder). Because this runs from WorkbookReader's static constructor, an unhandled + // throw here surfaces as a TypeInitializationException and permanently disables the reader + // for the whole AppDomain. Phase 1 above already registered plugins from loaded assemblies, + // so on enumeration failure we degrade to that result rather than crashing. This mirrors the + // per-path try/catch already used in the load loop below. + List referencedPaths; + try + { + referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll") + .Where(path => !loadedPaths.Contains(path)) + .ToList(); + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"Failed to enumerate assemblies in {AppDomain.CurrentDomain.BaseDirectory}: {ex.Message}"); + return; + } foreach (string path in referencedPaths) {