Skip to content

Commit

Permalink
Fix #3056: WriteParameterReference was using the wrong parameter defi…
Browse files Browse the repository at this point in the history
…nition in static methods.
  • Loading branch information
siegfriedpammer committed Aug 26, 2023
1 parent 7414aae commit 32242c5
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions ICSharpCode.Decompiler/Disassembler/DisassemblerHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,32 +141,44 @@ public static string Escape(string identifier)
return $"'{EscapeString(identifier).Replace("'", "\\'")}'";
}

public static void WriteParameterReference(ITextOutput writer, MetadataReader metadata, MethodDefinitionHandle handle, int sequence)
public static void WriteParameterReference(ITextOutput writer, MetadataReader metadata, MethodDefinitionHandle handle, int index)
{
var methodDefinition = metadata.GetMethodDefinition(handle);
var signature = methodDefinition.DecodeSignature(new FullTypeNameSignatureDecoder(metadata), default);
var parameters = methodDefinition.GetParameters().Select(p => metadata.GetParameter(p)).ToArray();
var signatureHeader = signature.Header;
int index = sequence;
if (signatureHeader.IsInstance && signature.ParameterTypes.Length == parameters.Length)
string name = GetParameterName(index);
if (name == null)
{
index--;
writer.WriteLocalReference(index.ToString(), "param_" + index);
}
if (index < 0 || index >= parameters.Length)
else
{
writer.WriteLocalReference(sequence.ToString(), "param_" + index);
writer.WriteLocalReference(name, "param_" + index);
}
else

string GetParameterName(int parameterNumber)
{
var param = parameters[index];
if (param.Name.IsNil)
var methodDefinition = metadata.GetMethodDefinition(handle);
if ((methodDefinition.Attributes & System.Reflection.MethodAttributes.Static) != 0)
{
writer.WriteLocalReference(sequence.ToString(), "param_" + index);
parameterNumber++;
}
else
foreach (var p in methodDefinition.GetParameters())
{
writer.WriteLocalReference(Escape(metadata.GetString(param.Name)), "param_" + index);
var param = metadata.GetParameter(p);
if (param.SequenceNumber < parameterNumber)
{
continue;
}
else if (param.SequenceNumber == parameterNumber)
{
if (param.Name.IsNil)
return null;
return Escape(metadata.GetString(param.Name));
}
else
{
break;
}
}
return null;
}
}

Expand Down

0 comments on commit 32242c5

Please sign in to comment.