Skip to content

Commit

Permalink
fix few null dereference issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxology committed Mar 20, 2024
1 parent 2fab3b3 commit 2a07a00
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions DotNetAstGen/PDBWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This is created to allow PDB generation functionality via a self-contained DotNetAstGen binary. Original PDB
// generation tool ilspycmd from which this is built required presence of dependent DLLs and was installed as a
// dotnet tool. This brings core PDB generation. Derived from ILSpy Code at following locations:
// dotnet tool. Derived from ILSpy Code at following locations:
// -- https://github.com/icsharpcode/ILSpy/blob/release/8.1/ICSharpCode.Decompiler/DebugInfo/DebugInfoGenerator.cs
// -- https://github.com/icsharpcode/ILSpy/blob/release/8.1/ICSharpCode.Decompiler/DebugInfo/PortablePdbWriter.cs
//
Expand Down Expand Up @@ -255,7 +255,7 @@ void HandleMethodBody(ILFunction function, MethodBodyBlock methodBody)
// we don't really need variables, but need to add empty set
var localVariables = new HashSet<ILVariable>(ILVariableKeyComparer);

LocalScopes.Add(((MethodDefinitionHandle)method.MetadataToken, currentImportScope,
LocalScopes.Add(((MethodDefinitionHandle)method!.MetadataToken, currentImportScope,
0, methodBody.GetCodeSize(), localVariables));
}
}
Expand Down Expand Up @@ -321,7 +321,7 @@ public static void WritePdb(
{
MetadataBuilder metadata = new MetadataBuilder();
MetadataReader reader = file.Metadata;
var entrypointHandle = MetadataTokens.MethodDefinitionHandle(file.Reader.PEHeaders.CorHeader.EntryPointTokenOrRelativeVirtualAddress);
var entrypointHandle = MetadataTokens.MethodDefinitionHandle(file!.Reader.PEHeaders.CorHeader.EntryPointTokenOrRelativeVirtualAddress);

Check warning on line 324 in DotNetAstGen/PDBWriter.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 324 in DotNetAstGen/PDBWriter.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

var sequencePointBlobs = new Dictionary<MethodDefinitionHandle, (DocumentHandle Document, BlobHandle SequencePoints)>();
var emptyList = new List<ICSharpCode.Decompiler.DebugInfo.SequencePoint>();
Expand Down Expand Up @@ -368,6 +368,10 @@ string BuildFileNameFromTypeName(TypeDefinitionHandle handle)

// Generate sequence points for the syntax tree
var sequencePoints = decompiler.CreateSequencePoints(syntaxTree);
if (sequencePoints == null)
{
continue;
}

// Generate other debug information
var debugInfoGen = new DebugInfoGenerator(decompiler.TypeSystem);
Expand Down Expand Up @@ -396,14 +400,14 @@ string BuildFileNameFromTypeName(TypeDefinitionHandle handle)
foreach (var function in debugInfoGen.Functions)
{
var method = function.MoveNextMethod ?? function.Method;
var methodHandle = (MethodDefinitionHandle)method.MetadataToken;
var methodHandle = (MethodDefinitionHandle)method!.MetadataToken;
sequencePoints.TryGetValue(function, out var points);
ProcessMethod(methodHandle, document, points, syntaxTree);
if (function.MoveNextMethod != null)
{
stateMachineMethods.Add((
(MethodDefinitionHandle)function.MoveNextMethod.MetadataToken,
(MethodDefinitionHandle)function.Method.MetadataToken
(MethodDefinitionHandle)function.Method!.MetadataToken
));
customDebugInfo.Add((
function.MoveNextMethod.MetadataToken,
Expand Down Expand Up @@ -455,7 +459,7 @@ string BuildFileNameFromTypeName(TypeDefinitionHandle handle)
foreach (var local in localScope.Locals.OrderBy(l => l.Index))
{
var localVarName = local.Name != null ? metadata.GetOrAddString(local.Name) : default;
metadata.AddLocalVariable(LocalVariableAttributes.None, local.Index.Value, localVarName);
metadata.AddLocalVariable(LocalVariableAttributes.None, local.Index!.Value, localVarName);
}

metadata.AddLocalScope(localScope.Method, localScope.Import.Handle, firstLocalVariable,
Expand Down Expand Up @@ -491,7 +495,7 @@ string BuildFileNameFromTypeName(TypeDefinitionHandle handle)
blobBuilder.WriteContentTo(targetStream);

void ProcessMethod(MethodDefinitionHandle method, DocumentHandle document,
List<ICSharpCode.Decompiler.DebugInfo.SequencePoint> sequencePoints, SyntaxTree syntaxTree)
List<ICSharpCode.Decompiler.DebugInfo.SequencePoint>? sequencePoints, SyntaxTree syntaxTree)
{
var methodDef = reader.GetMethodDefinition(method);
int localSignatureRowId;
Expand All @@ -503,7 +507,6 @@ void ProcessMethod(MethodDefinitionHandle method, DocumentHandle document,
}
else
{
methodBody = null;
localSignatureRowId = 0;
}

Expand Down

0 comments on commit 2a07a00

Please sign in to comment.