From a1205d40e9d7d5f091a08b42c46a9a0dc96e7989 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sun, 22 May 2011 13:56:44 +0200 Subject: [PATCH] Fix #168: ILSpy crashes on close when viewing an obfuscated assembly --- ILSpy/ILSpySettings.cs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/ILSpy/ILSpySettings.cs b/ILSpy/ILSpySettings.cs index 7d0fd19cb..053bee7ce 100644 --- a/ILSpy/ILSpySettings.cs +++ b/ILSpy/ILSpySettings.cs @@ -19,6 +19,7 @@ using System; using System.IO; using System.Linq; +using System.Text; using System.Threading; using System.Xml; using System.Xml.Linq; @@ -58,7 +59,7 @@ namespace ICSharpCode.ILSpy { using (new MutexProtector(ConfigFileMutex)) { try { - XDocument doc = XDocument.Load(GetConfigFile()); + XDocument doc = LoadWithoutCheckingCharacters(GetConfigFile()); return new ILSpySettings(doc.Root); } catch (IOException) { return new ILSpySettings(); @@ -68,6 +69,15 @@ namespace ICSharpCode.ILSpy } } + static XDocument LoadWithoutCheckingCharacters(string fileName) + { + // XDocument.Load(fileName) validates that no invalid characters appear (not even in escaped form), + // but we need those characters for some obfuscated assemblies. + using (XmlTextReader r = new XmlTextReader(fileName)) { + return XDocument.Load(r); + } + } + /// /// Saves a setting section. /// @@ -94,7 +104,7 @@ namespace ICSharpCode.ILSpy string config = GetConfigFile(); XDocument doc; try { - doc = XDocument.Load(config); + doc = LoadWithoutCheckingCharacters(config); } catch (IOException) { // ensure the directory exists Directory.CreateDirectory(Path.GetDirectoryName(config)); @@ -104,7 +114,12 @@ namespace ICSharpCode.ILSpy } doc.Root.SetAttributeValue("version", RevisionClass.Major + "." + RevisionClass.Minor + "." + RevisionClass.Build + "." + RevisionClass.Revision); action(doc.Root); - doc.Save(config); + // We can't use XDocument.Save(filename) because that checks for invalid characters, but those can appear + // in obfuscated assemblies. + using (XmlTextWriter writer = new XmlTextWriter(config, Encoding.UTF8)) { + writer.Formatting = Formatting.Indented; + doc.Save(writer); + } } }