Skip to content

Commit ca4ba9b

Browse files
authored
Add sanity check for method counters (#174)
1 parent c253e0d commit ca4ba9b

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

MetadataProcessor.Shared/Tables/nanoTypeDefinitionTable.cs

+23-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,12 @@ protected override void WriteSingleItem(
143143
}
144144
}
145145

146-
WriteMethodBodies(item.Methods, item.Interfaces, writer);
146+
WriteMethodBodies(
147+
item.FullName,
148+
item.Methods,
149+
item.Interfaces,
150+
writer
151+
);
147152

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

@@ -227,6 +232,7 @@ private void WriteClassFields(
227232
}
228233

229234
private void WriteMethodBodies(
235+
string typeName,
230236
Collection<MethodDefinition> methods,
231237
Collection<InterfaceImplementation> iInterfaces,
232238
nanoBinaryWriter writer)
@@ -265,6 +271,22 @@ private void WriteMethodBodies(
265271

266272
writer.WriteUInt16(firstMethodId);
267273

274+
// sanity checks
275+
if (virtualMethodsCount > byte.MaxValue)
276+
{
277+
throw new InvalidOperationException($"Fatal error processing '{typeName}', virtual methods count ({virtualMethodsCount}) exceeds maximum supported (255).");
278+
}
279+
280+
if (instanceMethodsCount > byte.MaxValue)
281+
{
282+
throw new InvalidOperationException($"Fatal error processing '{typeName}', instance methods count ({instanceMethodsCount}) exceeds maximum supported (255).");
283+
}
284+
285+
if (staticMethodsCount > byte.MaxValue)
286+
{
287+
throw new InvalidOperationException($"Fatal error processing '{typeName}', static methods count ({staticMethodsCount}) exceeds maximum supported (255).");
288+
}
289+
268290
writer.WriteByte((byte)virtualMethodsCount);
269291
writer.WriteByte((byte)instanceMethodsCount);
270292
writer.WriteByte((byte)staticMethodsCount);

0 commit comments

Comments
 (0)