Skip to content

Commit e28e43b

Browse files
committed
Add sanity check for method counters (#174)
(cherry picked from commit ca4ba9b)
1 parent 413cc3a commit e28e43b

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

MetadataProcessor.Shared/Tables/nanoTypeDefinitionTable.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,12 @@ protected override void WriteSingleItem(
161161
}
162162
}
163163

164-
WriteMethodBodies(item.Methods, item.Interfaces, writer);
164+
WriteMethodBodies(
165+
item.FullName,
166+
item.Methods,
167+
item.Interfaces,
168+
writer
169+
);
165170

166171
// Interfaces
167172
_context.SignaturesTable.WriteDataType(item, writer, false, true, true);
@@ -291,6 +296,7 @@ private void WriteClassFields(
291296
}
292297

293298
private void WriteMethodBodies(
299+
string typeName,
294300
Collection<MethodDefinition> methods,
295301
Collection<InterfaceImplementation> iInterfaces,
296302
nanoBinaryWriter writer)
@@ -331,6 +337,22 @@ private void WriteMethodBodies(
331337
// FirstMethod
332338
writer.WriteUInt16(firstMethodId);
333339

340+
// sanity checks
341+
if (virtualMethodsCount > byte.MaxValue)
342+
{
343+
throw new InvalidOperationException($"Fatal error processing '{typeName}', virtual methods count ({virtualMethodsCount}) exceeds maximum supported (255).");
344+
}
345+
346+
if (instanceMethodsCount > byte.MaxValue)
347+
{
348+
throw new InvalidOperationException($"Fatal error processing '{typeName}', instance methods count ({instanceMethodsCount}) exceeds maximum supported (255).");
349+
}
350+
351+
if (staticMethodsCount > byte.MaxValue)
352+
{
353+
throw new InvalidOperationException($"Fatal error processing '{typeName}', static methods count ({staticMethodsCount}) exceeds maximum supported (255).");
354+
}
355+
334356
// VirtualMethodCount
335357
writer.WriteByte((byte)virtualMethodsCount);
336358
// InstanceMethodCount

0 commit comments

Comments
 (0)