From 83a1b3f7d25cec23509a8b077e1b86e3822c9a9a Mon Sep 17 00:00:00 2001 From: Jeremy Pritts Date: Fri, 30 Sep 2022 16:36:35 -0400 Subject: [PATCH 1/2] Improve errors for leaf record access failures --- .../Records/CodeViewSymbol.cs | 16 ++++++++++++++++ .../Serialized/SerializedConstantSymbol.cs | 5 +---- .../SerializedUserDefinedTypeSymbol.cs | 5 +---- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/AsmResolver.Symbols.Pdb/Records/CodeViewSymbol.cs b/src/AsmResolver.Symbols.Pdb/Records/CodeViewSymbol.cs index 5869c3acd..0d695397f 100644 --- a/src/AsmResolver.Symbols.Pdb/Records/CodeViewSymbol.cs +++ b/src/AsmResolver.Symbols.Pdb/Records/CodeViewSymbol.cs @@ -1,4 +1,5 @@ using AsmResolver.IO; +using AsmResolver.Symbols.Pdb.Leaves; using AsmResolver.Symbols.Pdb.Records.Serialized; namespace AsmResolver.Symbols.Pdb.Records; @@ -37,4 +38,19 @@ public static CodeViewSymbol FromReader(PdbReaderContext context, ref BinaryStre _ => new UnknownSymbol(type, dataReader.ReadToEnd()) }; } + + private protected T? GetLeafRecord(PdbReaderContext context, uint typeIndex) where T : CodeViewLeaf + { + return context.ParentImage.TryGetLeafRecord(typeIndex, out var leaf) + ? leaf switch + { + T t => t, + UnknownCodeViewLeaf unknownLeaf => context.Parameters.ErrorListener.BadImageAndReturn( + $"{GetType().Name} contains an unknown leaf ({unknownLeaf.LeafKind}) at index {typeIndex:X8}."), + _ => context.Parameters.ErrorListener.BadImageAndReturn( + $"{GetType().Name} contains an incorrect leaf type ({leaf.GetType().Name}) at index {typeIndex:X8}.") + } + : context.Parameters.ErrorListener.BadImageAndReturn( + $"{GetType().Name} contains an invalid type index {typeIndex:X8}."); + } } diff --git a/src/AsmResolver.Symbols.Pdb/Records/Serialized/SerializedConstantSymbol.cs b/src/AsmResolver.Symbols.Pdb/Records/Serialized/SerializedConstantSymbol.cs index 3c3c31c53..4341bc9c8 100644 --- a/src/AsmResolver.Symbols.Pdb/Records/Serialized/SerializedConstantSymbol.cs +++ b/src/AsmResolver.Symbols.Pdb/Records/Serialized/SerializedConstantSymbol.cs @@ -31,9 +31,6 @@ public SerializedConstantSymbol(PdbReaderContext context, BinaryStreamReader rea /// protected override CodeViewTypeRecord? GetConstantType() { - return _context.ParentImage.TryGetLeafRecord(_typeIndex, out var leaf) && leaf is CodeViewTypeRecord type - ? type - : _context.Parameters.ErrorListener.BadImageAndReturn( - $"Constant contains an invalid type index {_typeIndex:X8}."); + return GetLeafRecord(_context, _typeIndex); } } diff --git a/src/AsmResolver.Symbols.Pdb/Records/Serialized/SerializedUserDefinedTypeSymbol.cs b/src/AsmResolver.Symbols.Pdb/Records/Serialized/SerializedUserDefinedTypeSymbol.cs index fdcce9a7f..330f4e0f6 100644 --- a/src/AsmResolver.Symbols.Pdb/Records/Serialized/SerializedUserDefinedTypeSymbol.cs +++ b/src/AsmResolver.Symbols.Pdb/Records/Serialized/SerializedUserDefinedTypeSymbol.cs @@ -30,9 +30,6 @@ public SerializedUserDefinedTypeSymbol(PdbReaderContext context, BinaryStreamRea /// protected override CodeViewTypeRecord? GetSymbolType() { - return _context.ParentImage.TryGetLeafRecord(_typeIndex, out var leaf) && leaf is CodeViewTypeRecord type - ? type - : _context.Parameters.ErrorListener.BadImageAndReturn( - $"User-defined type contains an invalid type index {_typeIndex:X8}."); + return GetLeafRecord(_context, _typeIndex); } } From 14028601ff57c1e380e25bf38257d46897164bae Mon Sep 17 00:00:00 2001 From: Jeremy Pritts Date: Thu, 13 Oct 2022 01:12:19 -0400 Subject: [PATCH 2/2] Update the error message --- src/AsmResolver.Symbols.Pdb/Records/CodeViewSymbol.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/AsmResolver.Symbols.Pdb/Records/CodeViewSymbol.cs b/src/AsmResolver.Symbols.Pdb/Records/CodeViewSymbol.cs index 0d695397f..408ddb4b8 100644 --- a/src/AsmResolver.Symbols.Pdb/Records/CodeViewSymbol.cs +++ b/src/AsmResolver.Symbols.Pdb/Records/CodeViewSymbol.cs @@ -46,11 +46,11 @@ public static CodeViewSymbol FromReader(PdbReaderContext context, ref BinaryStre { T t => t, UnknownCodeViewLeaf unknownLeaf => context.Parameters.ErrorListener.BadImageAndReturn( - $"{GetType().Name} contains an unknown leaf ({unknownLeaf.LeafKind}) at index {typeIndex:X8}."), + $"{CodeViewSymbolType} references a leaf at {typeIndex:X8} that is of an unknown type {unknownLeaf.LeafKind}."), _ => context.Parameters.ErrorListener.BadImageAndReturn( - $"{GetType().Name} contains an incorrect leaf type ({leaf.GetType().Name}) at index {typeIndex:X8}.") + $"{CodeViewSymbolType} references a leaf at {typeIndex:X8} that is of an unexpected type ({leaf.LeafKind}).") } : context.Parameters.ErrorListener.BadImageAndReturn( - $"{GetType().Name} contains an invalid type index {typeIndex:X8}."); + $"{CodeViewSymbolType} contains an invalid type index {typeIndex:X8}."); } }