diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/glbImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/glbImporter.cs index d6415a0de..a0d489672 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/glbImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/glbImporter.cs @@ -48,27 +48,33 @@ namespace UniGLTF public static List 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(); diff --git a/Assets/UniGLTF/Tests/UniGLTF/GlbParserTests.cs b/Assets/UniGLTF/Tests/UniGLTF/GlbParserTests.cs index 02de02b20..ae99dba8c 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/GlbParserTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/GlbParserTests.cs @@ -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(() => + Assert.Throws(() => { + // 再パース var data2 = new GlbBinaryParser(mod, Path.GetFileNameWithoutExtension(path)).Parse(); }); - - var a = 0; } } }