Skip to content

Add sanity check for method counters #174

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

Merged
merged 1 commit into from
Feb 11, 2025
Merged
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
24 changes: 23 additions & 1 deletion MetadataProcessor.Shared/Tables/nanoTypeDefinitionTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ protected override void WriteSingleItem(
}
}

WriteMethodBodies(item.Methods, item.Interfaces, writer);
WriteMethodBodies(
item.FullName,
item.Methods,
item.Interfaces,
writer
);

_context.SignaturesTable.WriteDataType(item, writer, false, true, true);

Expand Down Expand Up @@ -227,6 +232,7 @@ private void WriteClassFields(
}

private void WriteMethodBodies(
string typeName,
Collection<MethodDefinition> methods,
Collection<InterfaceImplementation> iInterfaces,
nanoBinaryWriter writer)
Expand Down Expand Up @@ -265,6 +271,22 @@ private void WriteMethodBodies(

writer.WriteUInt16(firstMethodId);

// sanity checks
if (virtualMethodsCount > byte.MaxValue)
{
throw new InvalidOperationException($"Fatal error processing '{typeName}', virtual methods count ({virtualMethodsCount}) exceeds maximum supported (255).");
}

if (instanceMethodsCount > byte.MaxValue)
{
throw new InvalidOperationException($"Fatal error processing '{typeName}', instance methods count ({instanceMethodsCount}) exceeds maximum supported (255).");
}

if (staticMethodsCount > byte.MaxValue)
{
throw new InvalidOperationException($"Fatal error processing '{typeName}', static methods count ({staticMethodsCount}) exceeds maximum supported (255).");
}

writer.WriteByte((byte)virtualMethodsCount);
writer.WriteByte((byte)instanceMethodsCount);
writer.WriteByte((byte)staticMethodsCount);
Expand Down