Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Leaf Record Errors #360

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/AsmResolver.Symbols.Pdb/Records/CodeViewSymbol.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AsmResolver.IO;
using AsmResolver.Symbols.Pdb.Leaves;
using AsmResolver.Symbols.Pdb.Records.Serialized;

namespace AsmResolver.Symbols.Pdb.Records;
Expand Down Expand Up @@ -37,4 +38,19 @@ public static CodeViewSymbol FromReader(PdbReaderContext context, ref BinaryStre
_ => new UnknownSymbol(type, dataReader.ReadToEnd())
};
}

private protected T? GetLeafRecord<T>(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<T>(
$"{CodeViewSymbolType} references a leaf at {typeIndex:X8} that is of an unknown type {unknownLeaf.LeafKind}."),
_ => context.Parameters.ErrorListener.BadImageAndReturn<T>(
$"{CodeViewSymbolType} references a leaf at {typeIndex:X8} that is of an unexpected type ({leaf.LeafKind}).")
}
: context.Parameters.ErrorListener.BadImageAndReturn<T>(
$"{CodeViewSymbolType} contains an invalid type index {typeIndex:X8}.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public SerializedConstantSymbol(PdbReaderContext context, BinaryStreamReader rea
/// <inheritdoc />
protected override CodeViewTypeRecord? GetConstantType()
{
return _context.ParentImage.TryGetLeafRecord(_typeIndex, out var leaf) && leaf is CodeViewTypeRecord type
? type
: _context.Parameters.ErrorListener.BadImageAndReturn<CodeViewTypeRecord>(
$"Constant contains an invalid type index {_typeIndex:X8}.");
return GetLeafRecord<CodeViewTypeRecord>(_context, _typeIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ public SerializedUserDefinedTypeSymbol(PdbReaderContext context, BinaryStreamRea
/// <inheritdoc />
protected override CodeViewTypeRecord? GetSymbolType()
{
return _context.ParentImage.TryGetLeafRecord(_typeIndex, out var leaf) && leaf is CodeViewTypeRecord type
? type
: _context.Parameters.ErrorListener.BadImageAndReturn<CodeViewTypeRecord>(
$"User-defined type contains an invalid type index {_typeIndex:X8}.");
return GetLeafRecord<CodeViewTypeRecord>(_context, _typeIndex);
}
}