For…in loop in GLTFParser can cause gltf model load to fail

Description of the problem

GLTFParser.prototype.loadMeshes and GLTFParser.prototype.loadNodes use for...in loops.

Since a for...in loop in JavaScript iterates over an objects properties, these can cause the parse to fail if there is a custom property added to the Array object elsewhere on the site. For example, in GLTFParser.prototype.loadMeshes there is:

for ( var name in primitives ) {
    var primitive = primitives[ name ];
    var geometry = geometries[ name ];

If another script does something like:

Array.prototype.newCustomProp = function (num) {
}

Then eventually name would equal newCustomProp and the attempt to find primitive/geometry will fail.

Three.js version
  • Dev
  • r87
Browser
  • All of them
  • Chrome
  • Firefox
  • Internet Explorer
OS
  • All of them
  • Windows
  • macOS
  • Linux
  • Android
  • iOS

Author: Fantashit

1 thought on “For…in loop in GLTFParser can cause gltf model load to fail

  1. If another script does something like:

    Array.prototype.newCustomProp = function (num) {}
    

    If you ever see someone doing that, invite him to quite the javascript world please 😉

Comments are closed.