diff --git a/src/AsmResolver.Symbols.Pdb/Records/CodeViewSymbol.cs b/src/AsmResolver.Symbols.Pdb/Records/CodeViewSymbol.cs index 5869c3acd..408ddb4b8 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( + $"{CodeViewSymbolType} references a leaf at {typeIndex:X8} that is of an unknown type {unknownLeaf.LeafKind}."), + _ => context.Parameters.ErrorListener.BadImageAndReturn( + $"{CodeViewSymbolType} references a leaf at {typeIndex:X8} that is of an unexpected type ({leaf.LeafKind}).") + } + : context.Parameters.ErrorListener.BadImageAndReturn( + $"{CodeViewSymbolType} 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); } }