From 89544864bf923001c508ed07120d1332d0d53deb Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Wed, 11 Jul 2018 14:24:17 +0200 Subject: [PATCH] Provide better exceptions and messages in case of errors. --- ICSharpCode.Decompiler/IL/ILReader.cs | 2 +- ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs | 2 +- ICSharpCode.Decompiler/Metadata/PEFile.cs | 2 ++ .../BamlBinaryReader.cs | 7 ++++--- .../XmlBamlReader.cs | 2 +- ILSpy/Analyzers/AnalyzeContextMenuEntry.cs | 2 +- ILSpy/Analyzers/AnalyzerSearchTreeNode.cs | 10 +++++++--- ILSpy/Images/Images.cs | 6 +++--- 8 files changed, 20 insertions(+), 13 deletions(-) diff --git a/ICSharpCode.Decompiler/IL/ILReader.cs b/ICSharpCode.Decompiler/IL/ILReader.cs index 02541bcb4..c7d544ba9 100644 --- a/ICSharpCode.Decompiler/IL/ILReader.cs +++ b/ICSharpCode.Decompiler/IL/ILReader.cs @@ -1589,7 +1589,7 @@ namespace ICSharpCode.Decompiler.IL return new LdTypeToken(typeSystem.ResolveAsType(token)); if (token.Kind.IsMemberKind()) return new LdMemberToken(typeSystem.ResolveAsMember(token)); - throw new NotImplementedException(); + throw new BadImageFormatException("Invalid metadata token for ldtoken instruction."); } } } diff --git a/ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs b/ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs index f3295c19d..888e377e6 100644 --- a/ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs +++ b/ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs @@ -100,7 +100,7 @@ namespace ICSharpCode.Decompiler.IL void CloneVariables() { - throw new NotImplementedException(); + throw new NotSupportedException("ILFunction.CloneVariables is currently not supported!"); } public override void WriteTo(ITextOutput output, ILAstWritingOptions options) diff --git a/ICSharpCode.Decompiler/Metadata/PEFile.cs b/ICSharpCode.Decompiler/Metadata/PEFile.cs index 0d9190df7..03a428eb6 100644 --- a/ICSharpCode.Decompiler/Metadata/PEFile.cs +++ b/ICSharpCode.Decompiler/Metadata/PEFile.cs @@ -43,6 +43,8 @@ namespace ICSharpCode.Decompiler.Metadata { this.FileName = fileName ?? throw new ArgumentNullException(nameof(fileName)); this.Reader = reader ?? throw new ArgumentNullException(nameof(reader)); + if (!reader.HasMetadata) + throw new ArgumentException("PE file does not contain any metadata!"); this.Metadata = reader.GetMetadataReader(); } diff --git a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/BamlBinaryReader.cs b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/BamlBinaryReader.cs index 9cd4cfd62..76dbfbba3 100644 --- a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/BamlBinaryReader.cs +++ b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/BamlBinaryReader.cs @@ -8,7 +8,6 @@ namespace Ricciolo.StylesExplorer.MarkupReflection { internal class BamlBinaryReader : BinaryReader { - // Methods public BamlBinaryReader(Stream stream) : base(stream) { @@ -16,7 +15,8 @@ namespace Ricciolo.StylesExplorer.MarkupReflection public virtual double ReadCompressedDouble() { - switch (this.ReadByte()) { + byte b = this.ReadByte(); + switch (b) { case 1: return 0; case 2: @@ -27,8 +27,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection return ReadInt32() * 1E-06; case 5: return this.ReadDouble(); + default: + throw new BadImageFormatException($"Unexpected byte sequence in ReadCompressedDouble: 0x{b:x}"); } - throw new NotSupportedException(); } public int ReadCompressedInt32() diff --git a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs index 0e5e864d7..179b885c2 100644 --- a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs +++ b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs @@ -467,7 +467,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection int s = reader.ReadInt32(); int t = reader.ReadInt32(); if (((r != 0x600000) || (s != 0x600000)) || (t != 0x600000)) - throw new NotSupportedException(); + throw new BadImageFormatException("Magic value mismatch!"); initialized = true; } diff --git a/ILSpy/Analyzers/AnalyzeContextMenuEntry.cs b/ILSpy/Analyzers/AnalyzeContextMenuEntry.cs index 88e480272..af51a38e8 100644 --- a/ILSpy/Analyzers/AnalyzeContextMenuEntry.cs +++ b/ILSpy/Analyzers/AnalyzeContextMenuEntry.cs @@ -85,7 +85,7 @@ namespace ICSharpCode.ILSpy.Analyzers AnalyzerTreeView.Instance.ShowOrFocus(new AnalyzedEventTreeNode(ed)); break; default: - throw new NotSupportedException(); + throw new ArgumentOutOfRangeException(nameof(entity), $"Entity {entity.GetType().FullName} is not supported."); } } } diff --git a/ILSpy/Analyzers/AnalyzerSearchTreeNode.cs b/ILSpy/Analyzers/AnalyzerSearchTreeNode.cs index 61f28cee7..a4c17c8ed 100644 --- a/ILSpy/Analyzers/AnalyzerSearchTreeNode.cs +++ b/ILSpy/Analyzers/AnalyzerSearchTreeNode.cs @@ -68,9 +68,13 @@ namespace ICSharpCode.ILSpy.Analyzers } } - AnalyzerTreeNode EntityTreeNodeFactory(IEntity result) + AnalyzerTreeNode EntityTreeNodeFactory(IEntity entity) { - switch (result) { + if (entity == null) { + throw new ArgumentNullException(nameof(entity)); + } + + switch (entity) { case ITypeDefinition td: return new AnalyzedTypeTreeNode(td) { Language = this.Language @@ -92,7 +96,7 @@ namespace ICSharpCode.ILSpy.Analyzers Language = this.Language }; default: - throw new NotSupportedException(); + throw new ArgumentOutOfRangeException(nameof(entity), $"Entity {entity.GetType().FullName} is not supported."); } } diff --git a/ILSpy/Images/Images.cs b/ILSpy/Images/Images.cs index 2bf680b3d..61ed764d8 100644 --- a/ILSpy/Images/Images.cs +++ b/ILSpy/Images/Images.cs @@ -168,7 +168,7 @@ namespace ICSharpCode.ILSpy baseImage = Images.StaticClass; break; default: - throw new NotSupportedException(); + throw new ArgumentOutOfRangeException(nameof(icon), $"TypeIcon.{icon} is not supported!"); } return baseImage; @@ -238,7 +238,7 @@ namespace ICSharpCode.ILSpy baseImage = Images.Event; break; default: - throw new NotSupportedException(); + throw new ArgumentOutOfRangeException(nameof(icon), $"MemberIcon.{icon} is not supported!"); } return baseImage; @@ -303,7 +303,7 @@ namespace ICSharpCode.ILSpy overlayImage = Images.OverlayCompilerControlled; break; default: - throw new NotSupportedException(); + throw new ArgumentOutOfRangeException(nameof(overlay), $"AccessOverlayIcon.{overlay} is not supported!"); } return overlayImage; }