Skip to content

Commit

Permalink
Catch ByteCodeException type for versions outside IKVM.ByteCode range.
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabii committed Jul 4, 2024
1 parent 5102cb3 commit 196d05c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
33 changes: 18 additions & 15 deletions src/IKVM.Runtime/ClassFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,18 @@ sealed partial class ClassFile
/// <returns></returns>
internal static string GetClassName(byte[] bytes, int offset, int length, out bool isstub)
{
return GetClassName(ClassReader.Read(bytes.AsMemory(offset, length)), out isstub);
try
{
return GetClassName(ClassReader.Read(bytes.AsMemory(offset, length)), out isstub);
}
catch (UnsupportedClassVersionException e)
{
throw new UnsupportedClassVersionError(e.Message);
}
catch (ByteCodeException e)
{
throw new ClassFormatError(e.Message);
}
}

/// <summary>
Expand All @@ -94,20 +105,12 @@ internal static string GetClassName(byte[] bytes, int offset, int length, out bo
/// <exception cref="ClassFormatError"></exception>
static string GetClassName(ClassReader reader, out bool isstub)
{
try
{
if (reader.Version < new ClassFormatVersion(45, 3) || reader.Version > 52)
throw new UnsupportedClassVersionError(reader.Version);
if (reader.Version < new ClassFormatVersion(45, 3) || reader.Version > 52)
throw new UnsupportedClassVersionError(reader.Version);

// this is a terrible way to go about encoding this information
isstub = reader.Constants.OfType<Utf8ConstantReader>().Any(i => i.Value == "IKVM.NET.Assembly");

return string.Intern(reader.This.Name.Value.Replace('/', '.'));
}
catch (ByteCodeException e)
{
throw new ClassFormatError(e.Message);
}
// this is a terrible way to go about encoding this information
isstub = reader.Constants.OfType<Utf8ConstantReader>().Any(i => i.Value == "IKVM.NET.Assembly");
return string.Intern(reader.This.Name.Value.Replace('/', '.'));
}

#endif
Expand Down Expand Up @@ -294,7 +297,7 @@ internal ClassFile(RuntimeContext context, ClassReader reader, string inputClass
for (int i = 0; i < reader.Attributes.Count; i++)
{
var attribute = reader.Attributes[i];

switch (GetConstantPoolUtf8String(utf8_cp, attribute.Info.Record.NameIndex))
{
case "Deprecated":
Expand Down
13 changes: 8 additions & 5 deletions src/IKVM.Tools.Importer/IkvmImporterInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,12 +1160,10 @@ private static void ArrayAppend<T>(ref T[] array, T[] append)
}
}

private static byte[] ReadFromZip(ZipArchiveEntry ze)
static byte[] ReadFromZip(ZipArchiveEntry ze)
{
using MemoryStream ms = new MemoryStream();

using Stream s = ze.Open();

s.CopyTo(ms);

return ms.ToArray();
Expand All @@ -1183,6 +1181,10 @@ static bool EmitStubWarning(RuntimeContext context, StaticCompiler compiler, Com
{
return false;
}
catch (ByteCodeException)

Check failure on line 1184 in src/IKVM.Tools.Importer/IkvmImporterInternal.cs

View workflow job for this annotation

GitHub Actions / Build IKVM

The type or namespace name 'ByteCodeException' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 1184 in src/IKVM.Tools.Importer/IkvmImporterInternal.cs

View workflow job for this annotation

GitHub Actions / Build IKVM

The type or namespace name 'ByteCodeException' could not be found (are you missing a using directive or an assembly reference?)
{
return false;
}

if (cf.IKVMAssemblyAttribute == null)
{
Expand Down Expand Up @@ -1214,8 +1216,7 @@ static bool IsExcludedOrStubLegacy(RuntimeContext context, StaticCompiler compil
{
try
{
bool stub;
string name = ClassFile.GetClassName(data, 0, data.Length, out stub);
var name = ClassFile.GetClassName(data, 0, data.Length, out var stub);
if (options.IsExcludedClass(name) || (stub && EmitStubWarning(context, compiler, options, data)))
{
// we use stubs to add references, but otherwise ignore them
Expand All @@ -1224,8 +1225,10 @@ static bool IsExcludedOrStubLegacy(RuntimeContext context, StaticCompiler compil
}
catch (ClassFormatError)
{

}
}

return false;
}

Expand Down

0 comments on commit 196d05c

Please sign in to comment.