glb parser が glb header の length をチェックし早期に throw するようにした

This commit is contained in:
ousttrue 2022-01-18 13:52:57 +09:00
parent 79cad1342d
commit 217cc552eb
2 changed files with 15 additions and 11 deletions

View File

@ -48,27 +48,33 @@ namespace UniGLTF
public static List<GlbChunk> ParseGlbChunks(Byte[] bytes)
{
if (bytes.Length == 0)
//
// glb header(12byte)
//
if (bytes.Length < 12)
{
throw new Exception("empty bytes");
throw new FormatException("not glb header");
}
int pos = 0;
if (Encoding.ASCII.GetString(bytes, 0, 4) != GLB_MAGIC)
{
throw new Exception("invalid magic");
throw new FormatException("invalid magic");
}
pos += 4;
var version = BitConverter.ToUInt32(bytes, pos);
if (version != GLB_VERSION)
{
Debug.LogWarningFormat("unknown version: {0}", version);
return null;
throw new FormatException($"unknown version: {version}");
}
pos += 4;
//var totalLength = BitConverter.ToUInt32(bytes, pos);
var totalLength = BitConverter.ToUInt32(bytes, pos);
if (bytes.Length < totalLength)
{
throw new System.IO.EndOfStreamException($"Not enough size: {bytes.Length} < {totalLength}");
}
pos += 4;
var chunks = new List<GlbChunk>();

View File

@ -63,16 +63,14 @@ namespace UniGLTF
var bytes = File.ReadAllBytes(path);
var data = new GlbBinaryParser(bytes, Path.GetFileNameWithoutExtension(path)).Parse();
// 2個目のチャンクを削る
// glb header + 1st chunk only
var mod = bytes.Take(12 + 8 + data.Chunks[0].Bytes.Count).ToArray();
// 再パース
Assert.Throws<IOException>(() =>
Assert.Throws<EndOfStreamException>(() =>
{
// 再パース
var data2 = new GlbBinaryParser(mod, Path.GetFileNameWithoutExtension(path)).Parse();
});
var a = 0;
}
}
}