# gltfFileLoader: Array length out of bounds

**URL:** https://forum.babylonjs.com/t/gltffileloader-array-length-out-of-bounds/45849
**Category:** Bugs
**Tags:** gltf, gltffileloader, loaders
**Created:** [November 22, 2023, 9:23am UTC](https://forum.babylonjs.com/t/gltffileloader-array-length-out-of-bounds/45849 "2023-11-22T09:23:20Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)
#### Post date: [November 22, 2023, 9:23am UTC](https://forum.babylonjs.com/t/gltffileloader-array-length-out-of-bounds/45849/1 "2023-11-22T09:23:20Z")

</div>

Env: Windows 10 x64 / Firefox 115

 ![devtools](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/3/7/3790dd956a83c004889b97ef9679fb451e2701ce.png)

Code:

> <https://github.com/BabylonJS/Babylon.js/blob/c30577433112e98f08d8097bb94c3b835c95f9c6/packages/dev/loaders/src/glTF/glTFFileLoader.ts#L49-L60>

Manually running `new Uint8Array(arrayBufferView.buffer, (arrayBufferView).byteOffset + byteOffset, byteLength) ` can get correct result.

 ![result](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/7/9/79e6e23a507ed2f69910ba80d6d39da408bca9e8.png)

Here is runtime vars in devtools.  
 ![vars](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/4/e/4eeb011716ea28b9573adcb8fc081b3748e2afa5.png)

It looks like the check `(arrayBufferView as Uint8Array).byteOffset + byteLength > arrayBufferView.byteLength` is incorrect.  
The model is loaded using `loadAssetContainerAsync`.

---

<div class="post-metadata">

### Author: ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)
#### Post date: [November 22, 2023, 9:37am UTC](https://forum.babylonjs.com/t/gltffileloader-array-length-out-of-bounds/45849/2 "2023-11-22T09:37:17Z")

</div>

ping @bghgary . It’s Thanksgiving so he may take a few days to answer.

---

<div class="post-metadata">

### Author: ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)
#### Post date: [November 22, 2023, 12:02pm UTC](https://forum.babylonjs.com/t/gltffileloader-array-length-out-of-bounds/45849/3 "2023-11-22T12:02:26Z")

</div>

I think the correct check should be:

> <https://github.com/BabylonJS/Babylon.js/pull/14540>
>
> See https://forum.babylonjs.com/t/gltffileloader-array-length-out-of-bounds/4584…9

---

<div class="post-metadata">

### Author: ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)
#### Post date: [November 27, 2023, 10:10pm UTC](https://forum.babylonjs.com/t/gltffileloader-array-length-out-of-bounds/45849/4 "2023-11-27T22:10:27Z")

</div>

@kzhsw Can you send the asset that was causing the issue originally? I don’t believe the fix from @Evgeni_Popov is the original intention for the code and will cause the code to read bytes that it shouldn’t.

---

<div class="post-metadata">

### Author: ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)
#### Post date: [November 27, 2023, 10:11pm UTC](https://forum.babylonjs.com/t/gltffileloader-array-length-out-of-bounds/45849/5 "2023-11-27T22:11:01Z")

</div>



---

<div class="post-metadata">

### Author: ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)
#### Post date: [November 28, 2023, 7:30am UTC](https://forum.babylonjs.com/t/gltffileloader-array-length-out-of-bounds/45849/6 "2023-11-28T07:30:21Z")

</div>

It’s fixed by the linked pr.  
To compare:  
before

> **[Babylon.js Playground](https://playground.babylonjs.com/?snapshot=refs%2Fpull%2F14537%2Fmerge#DZB5ZC)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

after

> **[Babylon.js Playground](https://playground.babylonjs.com/#DZB5ZC)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

### Author: ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)
#### Post date: [November 28, 2023, 10:51am UTC](https://forum.babylonjs.com/t/gltffileloader-array-length-out-of-bounds/45849/7 "2023-11-28T10:51:56Z")

</div>

It may not be the right/full fix, that’s why we would need access to the source asset to investigate the issue more.

---

<div class="post-metadata">

### Author: ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)
#### Post date: [November 29, 2023, 12:34am UTC](https://forum.babylonjs.com/t/gltffileloader-array-length-out-of-bounds/45849/8 "2023-11-29T00:34:39Z")

</div>

I’ve updated the range check to be what was intended. @kzhsw I made sure the PG you sent works, but can you make sure it is working for your full scenario?

[Fix incorrect range check when reading glTF buffer by bghgary · Pull Request #14557 · BabylonJS/Babylon.js (github.com)](https://github.com/BabylonJS/Babylon.js/pull/14557)

---

<div class="post-metadata">

### Author: ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)
#### Post date: [November 29, 2023, 12:58am UTC](https://forum.babylonjs.com/t/gltffileloader-array-length-out-of-bounds/45849/9 "2023-11-29T00:58:02Z")

</div>

I patched node\_modules like this and it works.

 ![patched](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/9/1/9153e0d143143084ac1335c6e96960197c88450e.png)

---

<div class="post-metadata">

### Author: ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)
#### Post date: [November 29, 2023, 4:38pm UTC](https://forum.babylonjs.com/t/gltffileloader-array-length-out-of-bounds/45849/10 "2023-11-29T16:38:10Z")

</div>


